Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?
Posted
Comments
Ziee-M 23-May-14 12:39pm    
Hi, you can use "AutoResetEvent" to achive your goal, using Wait() and notify().
Sergey Alexandrovich Kryukov 23-May-14 15:57pm    
Correct. There are two way: explicitly resetting the event object, or having it auto-reset. The only difference is: auto-reset allows to pass exactly one thread. This is impossible to simulate using manual reset (and any other way on the user level), because, before the reset is done, more than one thread can pass through.

Two our comments can be considered as the answer. Don't you want to post a formal answer, to close the issue?

—SA
Sergey Alexandrovich Kryukov 23-May-14 15:59pm    
However, you are mistaken in methods' names. There are SetEvent, ResetEvent and WaitForSingleObject.

Sorry, I failed to notice "std::thread". Anyway, I answered in terms of Windows API, and the use of std::thread is very similar.

—SA
Sergey Alexandrovich Kryukov 23-May-14 16:07pm    
What you tried cannot be done directly on the level of the user code. I explained it in my answer, after [EDIT].
—SA

1 solution

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.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900