12 #include <condition_variable> 21 virtual void execute() = 0;
24 template<
class ReturnType>
27 template<
class F,
class ... Args>
30 : task_(std::bind( f, args... ) ) {
33 virtual void execute()
override {
37 std::future<ReturnType> getFuture() {
38 return task_.get_future();
42 std::packaged_task<ReturnType()> task_;
50 for (
int i = 0; i < n; ++i )
59 for (
size_t i = 0; i < threads_.size(); ++i )
61 for (
size_t i = 0; i < threads_.size(); ++i )
72 void addWork(
Work* w ) {
73 DBG( 3,
"WQ: Adding work" );
74 std::lock_guard<std::mutex> lg( qMutex_ );
76 queueCV_.notify_one();
77 DBG( 3,
"WQ: Work available" );
80 Work* retrieveWork() {
81 DBG( 3,
"WQ: Retrieving work" );
82 std::unique_lock<std::mutex> lock( qMutex_ );
83 queueCV_.wait( lock, [
this]{
return !workQ_.empty(); } );
84 Work* w = workQ_.front();
87 DBG( 3,
"WQ: Work retrieved" );
94 threads_.push_back( std::thread( std::bind( &this->execute,
this ) ) );
101 std::vector<std::thread> threads_;
102 std::queue<Work*> workQ_;
104 std::condition_variable queueCV_;
109 WorkQueue() : tp_( ThreadPool::getInstance() ), workProcessed_(0) {}
115 void addWork(
Work* w ) {
122 size_t workProcessed_;
Definition: threadpool.h:107
Definition: threadpool.h:47
Definition: threadpool.h:17
Definition: threadpool.h:25