Thread overview
How to stop the Threadmill
Jan 28, 2013
Chris
Jan 28, 2013
qznc
Jan 28, 2013
Dicebot
Jan 28, 2013
Chris
January 28, 2013
I am sure this has been asked before, but I couldn't find a solution or a hint via Google: I use a separate thread to play a sound file. Everything works fine, except that I cannot tell the thread to stop what it is doing. It refuses to receive messages while it is doing what it's doing (even if I just run a simple test function with a long for-loop to make sure it's not a hardware issue). I should probably add that the thread is started from within the main loop of a socket that waits for input. If the input is "stop" it should tell the play-thread to stop, but nothing happens.
I can imagine that the thread is no longer visible to the main-thread, although I keep a reference to it. Any help or hint will be appreciated.
January 28, 2013
On Monday, 28 January 2013 at 10:18:27 UTC, Chris wrote:
> I am sure this has been asked before, but I couldn't find a solution or a hint via Google: I use a separate thread to play a sound file. Everything works fine, except that I cannot tell the thread to stop what it is doing. It refuses to receive messages while it is doing what it's doing (even if I just run a simple test function with a long for-loop to make sure it's not a hardware issue). I should probably add that the thread is started from within the main loop of a socket that waits for input. If the input is "stop" it should tell the play-thread to stop, but nothing happens.
> I can imagine that the thread is no longer visible to the main-thread, although I keep a reference to it. Any help or hint will be appreciated.

There is no Thread.kill/stop method, if you are looking for it.

Your thread should run a loop to check for termination repeatedly.

  while (this.running) {
    // do another chunk of work
  }

To terminate the thread, you need to toggle the running flag and wait for it to end.

  soundplayer.running = false;

If your "chunk of work" is blocked as it waits for IO or something, that might take a long time.
January 28, 2013
On Monday, 28 January 2013 at 10:18:27 UTC, Chris wrote:
> I am sure this has been asked before, but I couldn't find a solution or a hint via Google: I use a separate thread to play a sound file. Everything works fine, except that I cannot tell the thread to stop what it is doing. It refuses to receive messages while it is doing what it's doing (even if I just run a simple test function with a long for-loop to make sure it's not a hardware issue). I should probably add that the thread is started from within the main loop of a socket that waits for input. If the input is "stop" it should tell the play-thread to stop, but nothing happens.
> I can imagine that the thread is no longer visible to the main-thread, although I keep a reference to it. Any help or hint will be appreciated.

Wild guess: sounds likely that your "play thread" does some blocking processing and just never pauses to process messages. You need to explicitly say to the thread when to have a pause in a job and check incoming messages. You can kill the thread externally but that would be an abnormal termination, no destructors called and all the bad stuff.

That said, I assume some typical pthread-based code, details may differ depending on the implementation.
January 28, 2013
Thanks guys. I can get the functionality I need by simply using a shared flag that breaks out of the play loop if set to false. I thought it might be possible to implement the stop function by sending a message to the thread assuming that the thread "listens" to receive *while* executing its task.
January 29, 2013
On Mon, 28 Jan 2013 05:18:26 -0500, Chris <wendlec@tcd.ie> wrote:

> I am sure this has been asked before, but I couldn't find a solution or a hint via Google: I use a separate thread to play a sound file. Everything works fine, except that I cannot tell the thread to stop what it is doing. It refuses to receive messages while it is doing what it's doing (even if I just run a simple test function with a long for-loop to make sure it's not a hardware issue). I should probably add that the thread is started from within the main loop of a socket that waits for input. If the input is "stop" it should tell the play-thread to stop, but nothing happens.
> I can imagine that the thread is no longer visible to the main-thread, although I keep a reference to it. Any help or hint will be appreciated.

Threads do not automatically receive messages.  You have to check for them periodically.

Messages go into a mailbox until you query for them.

-Steve