www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How Stop Worker Thread if Owner Thread is Finished?

reply Marcone <marcone email.com> writes:
Because when the main thread is completed the worker thread 
continues to run.
Oct 26 2020
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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
prev sibling next sibling parent reply Johann Lermer <johann.lermer elvin.eu> writes:
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
parent Anonymouse <zorael gmail.com> writes:
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
prev sibling parent reply Marcone <marcone email.com> writes:
I want to use isDaemon to automatic stop worker thread if ownner 
thread is finished
Oct 27 2020
parent reply Rene Zwanenburg <renezwanenburg gmail.com> writes:
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 finished
Terminating 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
parent Marcone <marcone email.com> writes:
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:
 I want to use isDaemon to automatic stop worker thread if 
 ownner thread is finished
Terminating 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
isDaemon: A daemon thread is automatically terminated when all non-daemon threads have terminated.
Oct 27 2020