What can cause a conditional variable

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?

+4
source share
2 answers

My understanding of this topic, while I was studying it at the university, was that it would be too expensive in terms of performance to implement 100% safe state variables.

The wikipedia page for false awakenings has a quote from David R. Butenhof (author Programming with POSIX Threads) that says:

, , , () , , . , , . , ,

while - , , , .

, , , .

+2

, .

, , , , conditonal notify(), . , , , , , , .

+1

Source: https://habr.com/ru/post/1524119/


All Articles