|
|
@ -33,9 +33,8 @@ public:
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
// need to keep track of threads so we can join them
|
|
|
|
// need to keep track of threads so we can join them
|
|
|
|
std::vector< std::thread > _workers;
|
|
|
|
std::vector< std::thread > _workers;
|
|
|
|
// the task queue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the task queue
|
|
|
|
std::deque<std::pair<bool, weak_function_ptr> > _loop_handles;
|
|
|
|
std::deque<std::pair<bool, weak_function_ptr> > _loop_handles;
|
|
|
|
|
|
|
|
|
|
|
|
// synchronization
|
|
|
|
// synchronization
|
|
|
@ -69,7 +68,6 @@ inline thread_pool::thread_pool(size_t num_threads,
|
|
|
|
if( _worker_warmup_cb) _worker_warmup_cb();
|
|
|
|
if( _worker_warmup_cb) _worker_warmup_cb();
|
|
|
|
while( !_stop)
|
|
|
|
while( !_stop)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
shared_function_ptr handle;
|
|
|
|
shared_function_ptr handle;
|
|
|
|
auto handle_it = _loop_handles.begin();
|
|
|
|
auto handle_it = _loop_handles.begin();
|
|
|
|
bool is_busy = false;
|
|
|
|
bool is_busy = false;
|
|
|
@ -104,14 +102,12 @@ inline thread_pool::thread_pool(size_t num_threads,
|
|
|
|
if(handle && !is_busy)
|
|
|
|
if(handle && !is_busy)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool continue_loop = (*handle)();
|
|
|
|
bool continue_loop = (*handle)();
|
|
|
|
if(!continue_loop){
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
{
|
|
|
|
handle_it->first = false;
|
|
|
|
|
|
|
|
_loop_handles.erase(handle_it);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
handle_it->first = false;
|
|
|
|
handle_it->first = false;
|
|
|
|
|
|
|
|
if(!continue_loop)
|
|
|
|
|
|
|
|
_loop_handles.erase(handle_it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// not busy anymore. notify to other threads
|
|
|
|
// not busy anymore. notify to other threads
|
|
|
|
_condition.notify_one();
|
|
|
|
_condition.notify_one();
|
|
|
|