Skip to content

Instantly share code, notes, and snippets.

@AALEKH
Created August 6, 2016 16:01
Show Gist options
  • Select an option

  • Save AALEKH/da18e22e41e2901daf922fd9c296bab5 to your computer and use it in GitHub Desktop.

Select an option

Save AALEKH/da18e22e41e2901daf922fd9c296bab5 to your computer and use it in GitHub Desktop.
//
// Copyright (c) 2013 Juan Palacios juan.palacios.puyana@gmail.com
// Subject to the BSD 2-Clause License
// - see < http://opensource.org/licenses/BSD-2-Clause>
//
#include "Queue.h"
#include <iostream>
void produce(Queue<int>& q) {
for (int i = 0; i< 10000; ++i) {
std::cout << "Pushing " << i << "\n";
q.push(i);
}
}
void consume(Queue<int>& q, unsigned int id) {
for (int i = 0; i< 2500; ++i) {
auto item = q.pop();
std::cout << "Consumer " << id << " popped " << item << "\n";
}
}
#include <thread>
int main()
{
Queue<int> q;
using namespace std::placeholders;
// producer thread
std::thread prod1(std::bind(produce, std::ref(q)));
// consumer threads
std::thread consumer1(std::bind(&consume, std::ref(q), 1));
std::thread consumer2(std::bind(&consume, std::ref(q), 2));
std::thread consumer3(std::bind(&consume, std::ref(q), 3));
std::thread consumer4(std::bind(&consume, std::ref(q), 4));
prod1.join();
consumer1.join();
consumer2.join();
consumer3.join();
consumer4.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment