RE: [GD-Windows] Stopping a thread cleanly
Brought to you by:
vexxed72
|
From: Jon W. <hp...@mi...> - 2002-02-17 22:12:29
|
> Actually, I'm not giving it instructions -- it's completely autonomous
> based on the parameter passed to it which is a pointer to the shared
> data it needs to operate on. It doesn't have any communication
I don't see why a critical section would be necessary in that loop.
There's no shared state going on between the player and the killer,
except for the event, which is protected enough without the critical
section.
Anyway, the problem with this is that you delay shut-down (while
spinning, waiting for that global) for whatever your sleep time is.
DirectSound supports playback notifier events, so you can set an
event, say, every 100 milliseconds played, and use this event to
wake up and fill the buffer. Or you can use your "shutdown" event
with an appropriate time-out as your sleep thing, if you're OK
with polling.
playerThread:
for(;;) {
if( WaitForSingleObject( event, POLL_INTERVAL ) == WAIT_TIMEOUT ) {
break;
}
lock buffer, read disk, unlock buffer
}
stopper:
SetEvent( event );
WaitForSingleObject( playerThread, INFINITE );
Regarding declaring members or globals "volatile" when they're shared
between threads: that's what the keyword is for, so I don't see how
that is considered ugly (assuming you need that semantic)?
Cheers,
/ h+
|