October 26, 2016 Re: How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dm | On Wednesday, 26 October 2016 at 10:03:30 UTC, dm wrote:
> Why so strange default behavior do not kill other threads in case some of threads raise exception?
> But thanks anyway.
AFAIK, on posix you should join the child thread, and when you do, the stored exception is rethrown in the joining thread.
|
October 27, 2016 How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dm | Thanks all. I gues I must rewrote my app to send exeptions to other threads, use non blocking io, etc, etc. |
October 27, 2016 Re: How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dm | On 10/26/16 4:42 AM, 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?
Hm... what about:
import std.traits: Parameters;
auto mySpawn(F)(F func, Parameters!F params)
{
static auto callIt(F func, Parameters!F params)
{
try
{
return func(params);
}
catch(Throwable t)
{
// print the exception/error, e.g.:
import std.writeln;
writeln(e.toString());
// terminate program
import core.stdc.stdlib : exit;
exit(1);
}
}
return spawn(&callIt, func, params);
}
void main()
{
auto tID = mySpawn(&func);
...
}
-Steve
|
October 28, 2016 Re: How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 27 October 2016 at 13:37:29 UTC, Steven Schveighoffer wrote:
> Hm... what about:
> ...
Main thread still running.
|
October 28, 2016 Re: How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dm | On Friday, 28 October 2016 at 03:38:05 UTC, dm wrote: > On Thursday, 27 October 2016 at 13:37:29 UTC, Steven Schveighoffer wrote: >> Hm... what about: >> ... > > Main thread still running. Actually it's depends on compiler. With ldc2 main thread doesn't stop, but with dmd seems all ok: root@proxytest:~# ./newthread object.Exception@newthread.d(30): Everything is bad. ---------------- ??:? void newthread.func() [0x451f52] ??:? void newthread.mySpawn!(void function()*).mySpawn(void function()*).callIt(void function()*) [0x452037] ??:? void std.concurrency._spawn!(void function(void function()*)*, void function()*)._spawn(bool, void function(void function()*)*, void function()*).exec() [0x4529f4] ??:? void core.thread.Thread.run() [0x46f1f1] ??:? thread_entryPoint [0x46ef1b] ??:? [0x42d00a3] uncaught exception dwarfeh(224) fatal error Aborted |
October 28, 2016 Re: How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to dm | I found http://arsdnet.net/this-week-in-d/2016-aug-07.html Maybe it's helps me. |
October 28, 2016 Re: How to kill whole application if child thread raises an exception? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 27 October 2016 at 13:37:29 UTC, Steven Schveighoffer wrote:
>
> Hm... what about:
>
> import std.traits: Parameters;
>
> auto mySpawn(F)(F func, Parameters!F params)
> {
> static auto callIt(F func, Parameters!F params)
> {
> try
> {
> return func(params);
> }
> catch(Throwable t)
> {
> // print the exception/error, e.g.:
> import std.writeln;
> writeln(e.toString());
> // terminate program
> import core.stdc.stdlib : exit;
> exit(1);
> }
> }
>
> return spawn(&callIt, func, params);
> }
>
> void main()
> {
> auto tID = mySpawn(&func);
> ...
> }
>
> -Steve
I found the solution which works with dmd and ldc2:
...
import core.stdc.stdlib: _Exit;
_Exit(exitcode);
...
|
Copyright © 1999-2021 by the D Language Foundation