Advertisement Jump to content
Sign in to follow this  

Pthread Multiple Wait Objects

This topic is 3111 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've just started to work with pthreads while porting my simple job system to Linux and I've run into a small problem. I'm trying to wait for either a semaphore (mutex + condition in pthread), or another just basic mutex + condition.

Using the Win32 API I solved it like this:

HANDLE handles[2] = { thread->semaphore, thread->status_event };
WaitForMultipleObjects(sizeof(handles) / sizeof(handles[0]), FALSE, INFINITE);

The semaphore is used by the worker threads to see if there is any available work in the queue. The status event is used to terminate the thread when we're exiting.

I think it should be an easy fix, however I couldn't come up with one that had the same results as the Win32 solution. :-
It seems you can only wait on one condition at a time? It could be solved with a timed wait and a loop, but that wastes cpu cycles. I'd like a real idle wait solution like in Win32.


Share this post

Link to post
Share on other sites
Why use two handles?

Have one handle or semaphore or similar. If shutting down, first set some flag, then notify workers.

Workers must handle spurious wake ups anyway.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!