POSIX and Win32 waiting for multiple conditiions simultaneously

Started by
1 comment, last by C-Junkie 19 years, 6 months ago
In Win32, one can easily wait for all sync objects of some set to be set before continuing using the WaitForMultipleObjects API call. However, with POSIX pthreads, the functionality doesnt exist. I have only found one paper that attempts to explain how to simulate such functionality, but it is written horibly. (At least i can get the author's incoherent psuedo-crap) (http://developers.sun.com/solaris/articles/waitfor_api.pdf) I am implementing a read write lock based on this article. In its source code for its RWLockEx, it waits simultaneously for a mutex that syncs general access to the lock (to maintain lock consistancy) and an another mutex used for writer exclusion. Attempting to lock these items independantly one after the (even with a loop to check on lock-state consistancy) results in deadlock. Two threads will compete for a lock, both locking first the access lock and then the write mutex. the second waits on the write mutex, which prevents it from ever freeing its access mutex. in order for the 1st thread to release its write lock, it needs to lock the access mutex which is held in limbo by thread 2 waiting on thread 1. Thread 1 now waits and results in DEADLOCK. By simply waiting for both the mutexes at the same time, thread 2 would be completely blocked from ever getting far enough to deadlock with thread 1. Any susggestions on how to solve this issue would be awsome! Thanks....
Advertisement
Sounds like the "dining philosophers" problem. Iirc, the solution is to use a different synchronization object - a semaphore instead of a mutex. Something like that.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
There was an implementation of the pseudocode at the end of that pdf...

This topic is closed to new replies.

Advertisement