I implemented a simple ThreadPool that uses std::list<Tasks> mTasksfor a list of tasks.
All threads wait for the condition variable using the following code:
EnterCriticalSection(&mCriticalSection);
while(mTasks.size() ==0)
SleepConditionVariableCS(&mConditionVariable,&mCriticalSection, INFINITE);
Until someone adds something to the list, and then one of them wakes up.
I used some time, which checks that the task list is not empty, although the only way to wake up is to add a new task to the list (so that it cannot be empty), the reason I did it is written on MSDN:
Variable conditions are subject to false awakenings (not associated with a clear trace) and stolen awakenings (another thread manages to run up to the swollen thread). Therefore, you should double-check the predicate (usually in a while loop) after the sleep operation returns.
But what are these side awakenings that will awaken my variable?
source
share