Optimized the use of threads somewhat

This commit is contained in:
Strahinja Val Markovic 2012-05-10 22:35:08 -07:00
parent ba76e3f6e6
commit 6e1c7f8429
2 changed files with 18 additions and 12 deletions

View File

@ -39,14 +39,14 @@ namespace YouCompleteMe
namespace namespace
{ {
const unsigned int MAX_ASYNC_THREADS = 4; const unsigned int MAX_ASYNC_THREADS = 1;
const unsigned int MIN_ASYNC_THREADS = 2; const unsigned int MIN_ASYNC_THREADS = 1;
void ThreadMain( TaskStack &task_stack ) void ThreadMain( TaskStack &task_stack )
{ {
while ( true ) while ( true )
{ {
( *task_stack.Pop() )(); ( *task_stack.Get() )();
} }
} }
@ -167,7 +167,7 @@ Future Completer::CandidatesForQueryAndTypeAsync(
unique_future< AsyncResults > future = task->get_future(); unique_future< AsyncResults > future = task->get_future();
task_stack_.Push( task ); task_stack_.Set( task );
return Future( move( future ) ); return Future( move( future ) );
} }

View File

@ -30,32 +30,38 @@ class ConcurrentStack : boost::noncopyable
{ {
public: public:
void Push( const T& data ) // TODO: rename this class, it's not a stack anymore
ConcurrentStack() : empty_( true ) {}
void Set( const T& data )
{ {
{ {
boost::unique_lock< boost::mutex > lock( mutex_ ); boost::unique_lock< boost::mutex > lock( mutex_ );
stack_.push( data ); latest_ = data;
empty_ = false;
} }
condition_variable_.notify_one(); condition_variable_.notify_one();
} }
T Pop() T Get()
{ {
boost::unique_lock< boost::mutex > lock( mutex_ ); boost::unique_lock< boost::mutex > lock( mutex_ );
while ( stack_.empty() ) while ( empty_ )
{ {
condition_variable_.wait( lock ); condition_variable_.wait( lock );
} }
T result = stack_.top(); empty_ = true;
stack_.pop(); return latest_;
return result;
} }
private: private:
std::stack<T> stack_; // std::stack< T > stack_;
T latest_;
bool empty_;
boost::mutex mutex_; boost::mutex mutex_;
boost::condition_variable condition_variable_; boost::condition_variable condition_variable_;