Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

exception in multithread env

Moderators: kris

Posted: 03/21/09 01:22:28 Modified: 03/21/09 01:28:26

The following prog can't terminated, is there any way to terminate a multithread program or get the exception info at least? I think it's important when programming in mutlithread.

import tango.core.Thread; void test() {

while(1) {

throw new Exception("test");

}

}

void main() {

auto thread = new Thread(&test); thread.start();

while(1) {

Thread.sleep(1);

}

}

Author Message

Posted: 03/21/09 02:08:29

the main thread can examine the exception-state of an owned thread. Also, you can set the daemon() state of a thread, such that it will be terminated automatically at exit time (and hence halt the executable). By default, threads will keep running regardless of whether the primary thread has been interrupted or not.

Posted: 03/21/09 03:16:21

kris wrote:

the main thread can examine the exception-state of an owned thread. Also, you can set the daemon() state of a thread, such that it will be terminated automatically at exit time (and hence halt the executable). By default, threads will keep running regardless of whether the primary thread has been interrupted or not.

set demon state seems no help here, the exception is occured in sub thread which will be terminated, but the main thread is still running. so I think the only way is check state of an owned thread in the main thread, but how can I do that? I can't find a way to do this in tango.core.Thread.

Posted: 03/21/09 05:31:45

See

Thread.join()

Posted: 03/21/09 07:46:36

thread.join will wait thread to complete, so you can't test a thread state in a loop. the problem is the main thread can't run as a loop, i have to rewrite the code as follow:

void testa() {

while(1) {

throw new Exception("test");

}

}

void testb() {

while(1) {

//do some stuff Thread.sleep(1);

}

}

void main() {

auto thread1 = new Thread(&testa);

thread1.isDaemon = true;

thread1.start();

auto thread2 = new Thread(&testb);

thread2.isDaemon = true;

thread2.start();

thread1.join(); thread2.join();

}

this can resolve my problem, though add a thread unnecessary. I think it's better to terminated the program when a unhandled exception occured whatever.

kris, thanks for the help! The problem bored me some time indeed.

Posted: 03/22/09 02:12:24

well, I found a new problem, if I have 2 thread, and exception may happend in thread a or thread b at runtime, then i can't know call join of thread a or b first, so the program can't be terminated and no exception info displayed. Any suggestions?

Posted: 03/22/09 02:45:03

you can always extend/subclass Thread to do whatever application-specific things you need. Untested example:

class MyThread : Thread
{
    public char[] failed = null;

    override void run()
    {
        try {
            // implement thread code
            // ...
            } catch (Exception e)
                     failed = e.toString;
    }
}

Posted: 03/22/09 03:23:29

I think it's a little inconvenient and wandering the performance if catch exception per thread. Why tango can't force all thread exit when a unhandled exception occured?

anyway, it can solve my problem, thanks!

Posted: 03/22/09 04:27:48

Tango does not kill the primary thread when some other thread encounters an error. That would seem to be application-specific behavior :)