digitalmars.D.learn - How Stop Worker Thread if Owner Thread is Finished?
- Marcone (2/2) Oct 26 2020 Because when the main thread is completed the worker thread
- Imperatorn (4/6) Oct 27 2020 Please provide a code example. It's much easier to reason about.
- Johann Lermer (56/56) Oct 27 2020 You could tell your thread via a shared variable that main has
- Anonymouse (6/10) Oct 27 2020 If you're receiving you can listen for an OwnerTerminated message.
- Marcone (2/2) Oct 27 2020 I want to use isDaemon to automatic stop worker thread if ownner
- Rene Zwanenburg (10/12) Oct 27 2020 Terminating threads without properly unwinding the stack is
- Marcone (4/16) Oct 27 2020 isDaemon: A daemon thread is automatically terminated when all
Because when the main thread is completed the worker thread continues to run.
Oct 26 2020
On Tuesday, 27 October 2020 at 00:46:48 UTC, Marcone wrote:Because when the main thread is completed the worker thread continues to run.Please provide a code example. It's much easier to reason about. Are you creating a thread from a thread and want the 2nd spawned thread to be terminated when the 1st thread terminates?
Oct 27 2020
You could tell your thread via a shared variable that main has ended: import std.stdio; import std.concurrency; import core.thread; shared bool end = false; void thread () { for (;;) { Thread.sleep (500.msecs); synchronized { if (end) break; } } writeln ("thread ends"); } void main () { spawn (&thread); Thread.sleep (3.seconds); writeln ("main ends"); synchronized { end = true; } } or you could use the fact, that receiveTimeout throws an exception, when main ends (although I don't know if this is the intended behaviour; the manual just says that it throws an exception when the sending thread was terminated): import std.stdio; import std.concurrency; import core.thread; void thread () { for (;;) { try { receiveTimeout (500.msecs); } catch (Throwable) { break; } } writeln ("thread ends"); } void main () { auto tid = spawn (&thread); Thread.sleep (3.seconds); writeln ("main ends"); }
Oct 27 2020
On Tuesday, 27 October 2020 at 08:33:36 UTC, Johann Lermer wrote:or you could use the fact, that receiveTimeout throws an exception, when main ends (although I don't know if this is the intended behaviour; the manual just says that it throws an exception when the sending thread was terminated):If you're receiving you can listen for an OwnerTerminated message. bool shouldExit = receiveTimeout((-1).seconds, (OwnerTerminated o) {} ); https://run.dlang.io/is/2t50yS
Oct 27 2020
I want to use isDaemon to automatic stop worker thread if ownner thread is finished
Oct 27 2020
On Tuesday, 27 October 2020 at 11:36:42 UTC, Marcone wrote:I want to use isDaemon to automatic stop worker thread if ownner thread is finishedTerminating threads without properly unwinding the stack is generally a very bad idea. Most importantly the destructors of stucts on the stack won't run which can cause problems like locks not being released. This isn't specific to D either. For example Raymond Chen writes about the TerminateThread function in Windows here: https://devblogs.microsoft.com/oldnewthing/20150814-00/?p=91811 You should signal the thread somehow that it's time to stop working
Oct 27 2020
On Tuesday, 27 October 2020 at 12:16:01 UTC, Rene Zwanenburg wrote:On Tuesday, 27 October 2020 at 11:36:42 UTC, Marcone wrote:isDaemon: A daemon thread is automatically terminated when all non-daemon threads have terminated.I want to use isDaemon to automatic stop worker thread if ownner thread is finishedTerminating threads without properly unwinding the stack is generally a very bad idea. Most importantly the destructors of stucts on the stack won't run which can cause problems like locks not being released. This isn't specific to D either. For example Raymond Chen writes about the TerminateThread function in Windows here: https://devblogs.microsoft.com/oldnewthing/20150814-00/?p=91811 You should signal the thread somehow that it's time to stop working
Oct 27 2020