assert or execption case program hang in multi thread

Denis Koroskin 2korden at gmail.com
Sat Feb 21 01:00:15 PST 2009


On Sat, 21 Feb 2009 10:38:40 +0300, liyu <yunwind at msn.com> wrote:

> code as below, assert can't terminate the program when main thread is  
> running, is it a bug or i miss something?
>
> import tango.core.Thread;
> void test()
> {
> 	while(1) {
> 		assert(false);
> 		//throw new Exception("test");
> 	}
> }
>
> void main() {
> 	auto thread = new Thread(&test);
> 	thread.start();
>
>         //if i remove the 3 lines below, then every thing work as  
> expected
> 	while(1) {
> 		Thread.sleep(100);	}
> } 

I believe it is an expected behaviour. You second thread (which you create explicitly) is throwing an exception and thus gets terminated.
You main thread, however, continues to live. What you call 'hang' is your code which sleeps in an infinite loop:

while(1) {
    Thread.sleep(100);
}

Try putting some stuff into in (like, Stdout('!');) and you'll see that your thread didn't hang, it works perfectly.

If you remove the while (1) { Thread.sleep(100); }, your main thread quickly reachs the end of the main() and also gets terminated.

Your program finishs its execution when *all* your threads stop.




More information about the Digitalmars-d mailing list