How to kill whole application if child thread raises an exception?
Paolo Invernizzi via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Oct 26 04:29:10 PDT 2016
On Wednesday, 26 October 2016 at 08:42:02 UTC, dm wrote:
> Hi. I tried code below:
>
> import std.concurrency;
> import std.stdio;
>
> void func()
> {
> throw new Exception("I'm an exception");
> }
>
> void main()
> {
> auto tID = spawn(&func);
> foreach(line; stdin.byLine)
> send(tID, "");
> }
>
> I expect my application will die immediatly, but main thread
> still running and I don't see any errors.
> I want to kill all my threads if it is unhandled exception.
> How can I do that?
You need to link the threads, and at least one receive after the
child thread has implicitly sent the exception to the main thread:
import std.concurrency;
import std.stdio;
---
void func()
{
throw new Exception("I'm an exception");
}
void main()
{
auto tID = spawnLinked(&func);
receive((int dummy){});
}
---
/Paolo
More information about the Digitalmars-d-learn
mailing list