code cleanup

pull/278/head
davide 9 years ago
parent 1d3de4885a
commit 6d0f48763c

@ -36,8 +36,7 @@ private:
// the task queue // the task queue
std::deque< weak_function_ptr > _loop_handles; std::deque<std::pair<bool, weak_function_ptr> > _loop_handles;
std::deque< bool > _busy;
// synchronization // synchronization
std::mutex _mutex; std::mutex _mutex;
@ -70,9 +69,9 @@ 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;
size_t index = 0; auto handle_it = _loop_handles.begin();
bool is_busy = false; bool is_busy = false;
// find an handle // find an handle
@ -83,19 +82,21 @@ inline thread_pool::thread_pool(size_t num_threads,
for (size_t count=0; count<_loop_handles.size(); count++) for (size_t count=0; count<_loop_handles.size(); count++)
{ {
_index = (_index+1) % _loop_handles.size(); _index = (_index+1) % _loop_handles.size();
handle = _loop_handles[index].lock(); handle_it = _loop_handles.begin() + _index;
is_busy = _busy[index];
is_busy = handle_it->first;
handle = handle_it->second.lock();
// if the weak pointer points to a delated handle, remove it // if the weak pointer points to a delated handle, remove it
if(!handle ){ if(!handle ){
_loop_handles.erase( _loop_handles.begin() + index); _loop_handles.erase(handle_it);
_busy.erase ( _busy.begin() + index);
_index = (_index) % _loop_handles.size(); _index = (_index) % _loop_handles.size();
} }
else{ else{
_busy[index] = true; // mark as busy
handle_it->first = true;
} }
if( handle && !is_busy) break; if( handle && !is_busy ) break;
} }
} }
@ -103,12 +104,13 @@ inline thread_pool::thread_pool(size_t num_threads,
{ {
bool continue_loop = (*handle)(); bool continue_loop = (*handle)();
if(!continue_loop){ if(!continue_loop){
_loop_handles.erase( _loop_handles.begin() + index); std::unique_lock<std::mutex> lock(_mutex);
_busy.erase ( _busy.begin() + index); handle_it->first = false;
_loop_handles.erase(handle_it);
} }
else{ else{
std::unique_lock<std::mutex> lock(_mutex); std::unique_lock<std::mutex> lock(_mutex);
_busy[index] = false; handle_it->first = false;
} }
// not busy anymore. notify to other threads // not busy anymore. notify to other threads
_condition.notify_one(); _condition.notify_one();
@ -119,7 +121,6 @@ inline thread_pool::thread_pool(size_t num_threads,
_condition.wait(lock); _condition.wait(lock);
} }
} }
}
if( _worker_teardown_cb) _worker_teardown_cb(); if( _worker_teardown_cb) _worker_teardown_cb();
} }
); );
@ -129,8 +130,7 @@ inline void thread_pool::subscribe_handle(const shared_function_ptr &loop_handle
{ {
{ {
std::unique_lock<std::mutex> lock(_mutex); std::unique_lock<std::mutex> lock(_mutex);
_loop_handles.push_back( loop_handle ); _loop_handles.push_back( std::make_pair(false, weak_function_ptr(loop_handle)) );
_busy.push_back( false );
} }
_condition.notify_one(); _condition.notify_one();
} }

Loading…
Cancel
Save