Results 1 to 3 of 3

Thread: [RESOLVED] Use lots of threads or re-use the same one?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Resolved [RESOLVED] Use lots of threads or re-use the same one?

    I'm using a class based around winmm.dll functions to play a wave file, which could be up to 2 minutes long. I have it running in a separate thread from my GUI and it works fine. Once the wave file has finished playing, the thread that it was playing on is effectively finished with.

    I'm very new to threading; when the user wants to play another wave file, should I just start another thread, or is there some way to re-use the first one? It's conceivable the user might play 50 or more wave files in a "session". If each one is on a separate thread, does the garbage collector keep things tidy, or would my resource usage keep increasing? I might have missed it but I haven't yet found something that kills a thread and releases all its resources.

    Many thanks.
    Last edited by paulg4ije; Dec 8th, 2014 at 12:13 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Use lots of threads or re-use the same one?

    What is the thread doing that you felt the need to use a separate thread from the GUI, i.e. what commands are you sending to the API, and are you playing synchronously, or asynchronously?
    Personally, I do tend to use a single background thread to do repeated operations, rather than letting a thread run to completion and launching a new thread.
    It tends on what the nature of the use is. Discrete jobs that run in the background to completion I would use background workers for. A thread that is going to be handling asynchronous events continually for the life of the application, I would use a created background thread.

    As for cleanup, if you kicked off the thread, either as a background worker or a created background thread, if that thread did the API call as a synchronous call, then it would wait for the file to finish, then continue, and if the sub you're using for the background thread didn't have a loop, then the thread should have exited, so doesn't need to be killed.
    If you did the API call asynchronously and didn't have a loop, then the thread should have finished right away, and the API would continue to play the file until it finished, so again, there would be no need to kill a thread.

    You could launch a background thread and do have a loop, and in the loop, do a read on a concurrent queue. If there is nothing in the queue, the thread should suspend itself and wait. The GUI can then when it wants, enqueue a file to the queue, which would "wake up" the thread, and it would dequeue the entry, and send it to the API, then loop back and wait on the queue again.
    You could have overlapping files playing if desired.
    I did that for a demo game test, to allow the various shooting and hit and explosion type sounds to overlap and not cut each other off. The GUI would just "trigger" the sounds, and the background thread would handle the calls to the API.
    Last edited by passel; Dec 8th, 2014 at 01:02 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: Use lots of threads or re-use the same one?

    Thanks for that. I'm using waveOutOpen to play the wave file (actually a programmatically filled buffer of wav data), so I guess that would be running asynchronously. However, I need to know when playing has ended, so I'm looping until I get the MM_WOM_DONE callback message. I just tried using task manager to look at the number of threads running on my PC. Obviously this fluctuates a little in a multitasking Windows environment, but by closing down as many processes as possible I can see a definite effect when I call my wave player. The number of threads goes up by about 10 and then when finished playing it drops by roughly the same amount. I will need to do some more testing but I think my current use of threading is OK for my purposes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width