How can I implement something like pthreads pthread_cond_signal on WinOS. I don’t want to use the pthreads Win32 port for this as I also support Win64.
I’m working on a task-based system where a client request spawns n-tasks that spawn n-threads on the server. After the client thread has spawned all the server tasks I want it to enter a sleep state or some other thread state that does not consume any system resources. When all the server tasks have completed I want the client thread to be awoken and return the result(s) to the client application.
Edit:
I’ve thought about waiting for a conditional variable in some shared memory, and check the condvar in a loop, but don’t loops consume resources too? I could add a sleep() inside the loop but then I won’t get the real-time client request & server response – as a task could take 1/1000 of a second (but also multiple seconds).
The Win32 API supports condition variables on Vista and higher. See this MSDN page for the API and this one for example usage.
WakeConditionVariable()is equivalent topthread_cond_signal()andWakeAllConditionVariable()is equivalent topthread_cond_broadcast()That said, if you are on VS2012, I’d really recommend that you use the new C++11 threading library. It also has support for condition variables and since running on multiple platforms is important to you, using the standard library could help you eventually merge your codebases.