Hello all, I'm working on project using multiple std::threads performing a function each waiting on the same event before carrying out a particular function. After completing the function, I'd like the thread to have the thread resume waiting until the event occurs again. I've been trying to accomplish this using a conditional variable-but I cannot seem to make this work correctly. Is this an appropriate way to accomplish this goal, or should I pursue another design to accomplish this functionality?
The answer is actually in the comment to the question written by Zeee-M and myself.
This is what you can use:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682655%28v=vs.85%29.aspx
[
^
],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686211(v=vs.85).aspx
[
^
],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms685081(v=vs.85).aspx
[
^
],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032%28v=vs.85%29.aspx
[
^
].
Auto-reset event provides an additional feature which cannot be simulated with manual reset: when the event object is set, it can allow exactly one object to pass. If another object is in the queue, it will get to the wait state until another call to
SetEvent
.
[EDIT #1]
This problem cannot be solved using any conditional variable, and nothing else on the use level. It is implemented in the kernel of the OS, where the code has access to the preemption mechanism and the wait state of thread and the mechanism of waking them up. All this is hidden in the system, to allow only the indirect use through wait objects.
When the thread is put in the wait state, it is switched off and never scheduled back again to execution (it is excluded from the set of preempting threads) until is waken up. The timeout or the setting of an event object is two ways of waking up. Such thread is not spending any CPUI time until it is waken up. This cannot be done directly on the level of the user code.
[EDIT #2]
Sorry, I answered in terms of Windows API, did not pay attention that you mentioned
std::thread
. The use of
std::thread
means the standard API introduced in C++11. But all the ideas are the same; and the usage is very similar.
The use of condition variable is explained here:
http://www.cplusplus.com/reference/condition_variable/condition_variable
[
^
].
If this is what you used and if you still had some problems, you would need to create a code sample and explain your problem on that sample.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Two our comments can be considered as the answer. Don't you want to post a formal answer, to close the issue?
—SA