// ThreadPool.cc #include "threadpool.hh" #include "shared_queue.hh" #include #include #include #include void producer( const int id, SharedQueue& q, int max_t, int max_n ) { std::random_device d; std::mt19937 mt(d()); std::uniform_int_distribution<> distr(0., max_t); for ( int n = 0; n < max_n ; n++ ) { n = distr(mt); std::this_thread::sleep_for(std::chrono::milliseconds(n)); // Lock q so we can push a new element std::unique_lock lock(*q.mutex()); q.queue()->push(n); // Notify one of the consumers q.cv()->notify_one(); } } /* void consumer( const int id, SharedQueue q_lock(q_mutex); while ( q.queue()->empty() ) { // Wait until we get a signal from the condition variable q_cv->wait(q_lock); } // Get a value and pop it off b = q.front(); q.pop(); } //std::cout << " -- Consumer " << id << " : " << b << std::endl; // Slow down this thread to have a race between the consumers and the producers // This only works after we have successfully read an int std::this_thread::sleep_for(std::chrono::milliseconds(max_t)); } } */