• Advertisement
Sign in to follow this  

Pthread Multiple Wait Objects

This topic is 2775 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.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
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