SDL_CondWait

Being a slightly backward sort, I originally posted this message to
news.lokigames.com. It doesn’t appear to have been propagated from there, so
I’m posting it again to gmane.org. I apologise if it did appear and I’m just
suffering temporary blindness. My post:

I am attempting my first multithreaded program with SDL, having previous
experience with various specific OSs. But I am confused by SDL_CondWait. Why
must I specify a mutex? What I would like to do is have a main controller
thread, and lots of subordinate threads which will all block when they reach
a certain point, and which the main controller thread may then decide to
simultaneously unblock all of. In win32 I would achieve this with
CreateEvent/PulseEvent/WaitForSingleObject. The controller thread would
create some event, all subordinate threads would block on that event (via
WaitForSingleObject), then when the controller wanted them all to unblock it
would PulseEvent.

With SDL I had anticipated that I would use an SDL_cond - i.e. a conditional
variable. But for some reason I am only allowed to have a thread wait on a
conditional after finishing with a mutual exclusion? Why is this?

-Thomas

Being a slightly backward sort, I originally posted this message to
news.lokigames.com. It doesn’t appear to have been propagated from there, so
I’m posting it again to gmane.org. I apologise if it did appear and I’m just
suffering temporary blindness. My post:

I am attempting my first multithreaded program with SDL, having previous
experience with various specific OSs. But I am confused by SDL_CondWait. Why
must I specify a mutex? What I would like to do is have a main controller
thread, and lots of subordinate threads which will all block when they reach
a certain point, and which the main controller thread may then decide to
simultaneously unblock all of. In win32 I would achieve this with
CreateEvent/PulseEvent/WaitForSingleObject. The controller thread would
create some event, all subordinate threads would block on that event (via
WaitForSingleObject), then when the controller wanted them all to unblock it
would PulseEvent.

With SDL I had anticipated that I would use an SDL_cond - i.e. a conditional
variable. But for some reason I am only allowed to have a thread wait on a
conditional after finishing with a mutual exclusion? Why is this?

If you read the documentation you will notice that the mutex is
unlocked as part of making the thread wait on the condition. The mutex
must be locked before calling SDL_CondWait so that only one thread will
be in that function at a time. One way to look at it is that the code
for SDL_CondWait is critical section that is protected by that mutex.

The other explanation is that that is the way it has been done since
mutices were invented. That is not a good explanation, but it is true.

	Bob PendletonOn Thu, 2003-07-24 at 05:36, Thomas Harte wrote:

-Thomas


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±----------------------------------+

I am attempting my first multithreaded program with SDL, having previous
experience with various specific OSs. But I am confused by SDL_CondWait. Why
must I specify a mutex? What I would like to do is have a main controller
thread, and lots of subordinate threads which will all block when they reach
a certain point, and which the main controller thread may then decide to
simultaneously unblock all of. In win32 I would achieve this with
CreateEvent/PulseEvent/WaitForSingleObject. The controller thread would
create some event, all subordinate threads would block on that event (via
WaitForSingleObject), then when the controller wanted them all to unblock it
would PulseEvent.

With SDL I had anticipated that I would use an SDL_cond - i.e. a conditional
variable. But for some reason I am only allowed to have a thread wait on a
conditional after finishing with a mutual exclusion? Why is this?

-Thomas

Hello,

I am attempting my first multithreaded program with SDL, having previous
experience with various specific OSs. But I am confused by SDL_CondWait. Why
must I specify a mutex?

[…]

With SDL I had anticipated that I would use an SDL_cond - i.e. a conditional
variable. But for some reason I am only allowed to have a thread wait on a
conditional after finishing with a mutual exclusion? Why is this?

This is common to threading APIs, you will find that most modern
threading APIs only allow you to handle a cond-variable with a mutex.
The scenario is as follows:

You have one thread waiting on a conditional variable, and another
waking it with a notify call. But if the notify comes before the wait,
the first thread may never be woken, as the notify only works on threads
which are sleeping right then.

To work around this, you typically use a flag variable, which you set
when you call notify, and test before waiting. But if access to this
flag is unprotected, some race conditions can still occur, such as the
flag being tested, then set, then the second thread notifies, then the
first thread waits.

So the flag is protected with a mutex. The notifier locks, then sets the
flag, then notifies, then unlocks. The waiter locks, then tests, then
waits, then unlocks. Since waiting atomically unlocks the mutex, the
notifier can access it while the waiter is sleeping.

Since the original situation is a common pitfall, and this solution so
general, many threading APIs will, as I said, force you to use a mutex.
Otherwise, you might want to implement something like this, but might
forget to use the mutexed version of wait (one must exist, for
atomicity). So think of it as for your own good !-)

Hope this helps,

Ben