Re: returning from worker thread
Steve Russell wrote:
That's very helpful, Scott. Between our posts, I had tried tried a while
loop with SuspendThread. For my simple waveOut, it seems to be working just
fine. Is there anything fundamentally wrong with this approach? Mainly, I
am trying to get a firm grasp on such use of a thread. Also, if you get the
time, would you mind demonstrating a version that relies on messaging? I
want to be sure to get what you're saying about that, and these examples
together might just be the best way for me to see the picture most clearly.
// audio thread function
UINT AudioThreadFunc(LPVOID pParam)
CAudioFile* audiofile = (CAudioFile*)pParam;
if(WaitForSingleObject(audiofile->m_hndDone,INFINITE) != WAIT_OBJECT_0)
// error handling code goes here
I assume that the idea here is that the main thread will do ResumeThread
to start the next file. You have to be certain that it will
ResumeThread only after the SuspendThread is executed. I'm not sure you
can guarantee that. Using WaitForMultipleObjects would be preferable.
It suspends the thread until an event is set, and it doesn't matter
whether the event is set before or after WFMO is called. Furthermore,
WFMO gives you a way to use another event to start an orderly shutdown
at program close.
A message-driven thread (see Newcomer's tutorial) uses different
signaling between threads. In a message-driven thread you have the
message map instead of the "while(TRUE)". Messages get dispatched, a
message handler function runs, then returns to the message pump. (The
message pump suspends the thread until another message arrives.) You
send messages to the thread with PostThreadMessage instead of SetEvent.
The advantages are that multiple messages can queue up, and they each
carry a couple of parameters that get passed to the message handler.
Scott McPhillips [VC++ MVP]