Thread overview
How Stop Worker Thread if Owner Thread is Finished?
Oct 27, 2020
Marcone
Oct 27, 2020
Imperatorn
Oct 27, 2020
Johann Lermer
Oct 27, 2020
Anonymouse
Oct 27, 2020
Marcone
Oct 27, 2020
Rene Zwanenburg
Oct 27, 2020
Marcone
October 27, 2020
Because when the main thread is completed the worker thread continues to run.
October 27, 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?
October 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");
}


October 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
October 27, 2020
I want to use isDaemon to automatic stop worker thread if ownner thread is finished
October 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 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
October 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:
>> 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.