Click to See Complete Forum and Search --> : Problem with Multithreading..


overtime_e
December 27th, 2004, 01:25 PM
Hello everyone,

i have a problem with multithread programming.. my program needs to play a wav file when a variable reaches a certain value.. i have used multithreading to play the wav file.. my aim is to make the program plays the wav file while it is still executing other events..

my problem is: the wav file only starts to play at the end of the program though the i have instructed it to start earlier..

skeleton of my code:

int main(void)
{
... //some codes
while ( ... )
{
...
...
Count = Count + 1; // Count = 0 initially
if (Count == 20)
{
_beginthread(PlayingWav, 0, NULL);
}

...
...
}
... //last line of codes
}

//PlayingWav function
void PlayingWav(void *dummy)
{
PlaySound("D:\\Temp\\song.wav", 0, SND_SYNC | SND_FILENAME);
}

everything runs smoothly but the song.wav is only played at the last line of my program.. can somebody tell me why? thanks thanks

OReubens
December 27th, 2004, 01:41 PM
You can start playing a wav file, and have your program continue. This is called an asynchronous play. You call the PlaySound() with an SND_ASYNC flag. As such, you do not have to start a thread to play the wav, or have your app wait on the wav playing.


It takes a while (several hundred microseconds) to actually launch a thread. The code following the _beginthread() probably takes fewer time than what it takes to actually get the new thread going.

If playing the wav is all you need... Replace the _beginthread() call with the call to PlaySound(), and replace the SND_SYNC flag with SND_ASYNC.

overtime_e
December 28th, 2004, 12:20 AM
Thanks OReubens, i will try that and see how!!

overtime_e
December 28th, 2004, 12:40 AM
Hello everyone,

i still need the _beginthread to work properly however in my case.. it can take more than 2 or 3 seconds before the thread starts to load, does anybody know why?

Andreas Masur
December 28th, 2004, 06:09 AM
Well...without seeing the code....no....

Andreas Masur
December 28th, 2004, 06:19 AM
[ Moved thread ]

OReubens
December 28th, 2004, 07:53 AM
i still need the _beginthread to work properly however in my case.. it can take more than 2 or 3 seconds before the thread starts to load, does anybody know why?

The most obvious reason... Because the thread that is launching the 2nd thread continues to keep doing work.
This may even be enhanced/exagerated if the main thread is running at a higher priority level than the newly launched thread. Are you playing around with functions to increase/lower priority ?

There are a variety of other reasons, like the fact your PC could be heavily loaded already. Or may have few memory left, which would cause a lot of paging before the thread could actually start.

Note that PlaySound itself also needs a bit of startup time. And you may get the idea the time is longer than it really is if the WAV you're playing starts with some silence.

Typically, the actual thread function should start about a quarter second after it was launched from the thread that created it. But this can be faster/slower depending on system load, loaded DLL's in the process space, .... Note that the new thread has already been doing quite a bit of work before it gets to the actual thread function. Like registering itself in each DLL, possibly allocating thread local storage and so on.