Jump to page: 1 2
Thread overview
How to kill whole application if child thread raises an exception?
Oct 26, 2016
dm
Oct 26, 2016
rikki cattermole
Oct 26, 2016
dm
Oct 26, 2016
rikki cattermole
Oct 26, 2016
Basile B.
Oct 26, 2016
dm
Oct 26, 2016
rikki cattermole
Oct 26, 2016
dm
Oct 26, 2016
Kagamin
Oct 26, 2016
Paolo Invernizzi
Oct 26, 2016
Adam D. Ruppe
Oct 27, 2016
dm
Oct 28, 2016
dm
Oct 28, 2016
dm
Oct 28, 2016
dm
Oct 28, 2016
dm
October 26, 2016
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?
October 26, 2016
On 26/10/2016 9:42 PM, 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?

Simple, handle the exceptions on each thread.
October 26, 2016
On Wednesday, 26 October 2016 at 08:53:13 UTC, rikki cattermole wrote:
> Simple, handle the exceptions on each thread.

I don't want handle exceptions. I want my application crash with exception description. Can you change my code above to show how it can be made?
October 26, 2016
Basically when you spawn a thread giving the function, you pass it through another function which will catch any exceptions not normally caught.

Of course this really should be the default behavior but somebody else may be more of a help here.

And it is pseudo code, so please don't expect it to 100% work as I have written it.

```D
void entryPoint(alias func)() {
	try {
		func();
	} catch (Exception e) {
		import std.stdio;
		writeln(e.toString());
	}
}

void main() {
	auto tid = spawn(&entryPoint!someFunc);
	// ...
	
}
```
October 26, 2016
On Wednesday, 26 October 2016 at 09:43:10 UTC, rikki cattermole wrote:
> Basically when you spawn a thread giving the function, you pass it through another function which will catch any exceptions not normally caught.
>
> Of course this really should be the default behavior but somebody else may be more of a help here.
>
> And it is pseudo code, so please don't expect it to 100% work as I have written it.
>
> ```D
> void entryPoint(alias func)() {
> 	try {
> 		func();
> 	} catch (Exception e) {
> 		import std.stdio;
> 		writeln(e.toString());
> 	}
> }
>
> void main() {
> 	auto tid = spawn(&entryPoint!someFunc);
> 	// ...
> 	
> }
> ```

It doesn't return. It still have to be killed by hand. (at least on Linux)
October 26, 2016
On Wednesday, 26 October 2016 at 09:43:10 UTC, rikki cattermole wrote:
> ```D
> void entryPoint(alias func)() {
> 	try {
> 		func();
> 	} catch (Exception e) {
> 		import std.stdio;
> 		writeln(e.toString());
> 	}
> }
>
> void main() {
> 	auto tid = spawn(&entryPoint!someFunc);
> 	// ...
> 	
> }
> ```

Well... This code shows me:
object.Exception@thread.d(6): I'm an exception
----------------

But my main thread still working :(
Why so strange default behavior do not kill other threads in case some of threads raise exception?
But thanks anyway.
October 26, 2016
On 26/10/2016 11:03 PM, dm wrote:
> On Wednesday, 26 October 2016 at 09:43:10 UTC, rikki cattermole wrote:
>> ```D
>> void entryPoint(alias func)() {
>>     try {
>>         func();
>>     } catch (Exception e) {
>>         import std.stdio;
>>         writeln(e.toString());
>>     }
>> }
>>
>> void main() {
>>     auto tid = spawn(&entryPoint!someFunc);
>>     // ...
>>
>> }
>> ```
>
> Well... This code shows me:
> object.Exception@thread.d(6): I'm an exception
> ----------------
>
> But my main thread still working :(
> Why so strange default behavior do not kill other threads in case some
> of threads raise exception?
> But thanks anyway.

If you throw an error it should crash the entire application.
But really you need to set up sync points within your application to allow it to die gracefully.
October 26, 2016
On Wednesday, 26 October 2016 at 10:09:05 UTC, rikki cattermole wrote:
> If you throw an error it should crash the entire application.
> But really you need to set up sync points within your application to allow it to die gracefully.

I tried throw new Error... But main thread still working.
Tried with dmd v2.071.2 and ldc2 0.17.2. OS - Linux.
October 26, 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
October 26, 2016
On Wednesday, 26 October 2016 at 08:42:02 UTC, dm wrote:
> I want to kill all my threads if it is unhandled exception.
> How can I do that?

The spawnLinked function might help:

http://dpldocs.info/experimental-docs/std.concurrency.spawnLinked.html

http://dlang.org/phobos/std_concurrency.html#spawnLinked

" This new thread is linked to the calling thread so that if either it or the calling thread terminates a LinkTerminated message will be sent to the other, causing a LinkTerminated exception to be thrown on receive()."


At least that sounds basically right.
« First   ‹ Prev
1 2