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

View File

@ -30,32 +30,38 @@ class ConcurrentStack : boost::noncopyable
{
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_ );
stack_.push( data );
latest_ = data;
empty_ = false;
}
condition_variable_.notify_one();
}
T Pop()
T Get()
{
boost::unique_lock< boost::mutex > lock( mutex_ );
while ( stack_.empty() )
while ( empty_ )
{
condition_variable_.wait( lock );
}
T result = stack_.top();
stack_.pop();
return result;
empty_ = true;
return latest_;
}
private:
std::stack<T> stack_;
// std::stack< T > stack_;
T latest_;
bool empty_;
boost::mutex mutex_;
boost::condition_variable condition_variable_;