| Thread overview | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
February 21, 2009 assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
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); }
}
| ||||
February 21, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to liyu | On Sat, 21 Feb 2009 10:38:40 +0300, liyu <yunwind@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.
| |||
February 21, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to liyu | maybe i should title it assert or execption can't terminate the prog in multithread:-) yes, the main thread is still running, but i want the program terminated when a assert failure or exception happened whatever. otherwise i have to notify the main thread, i think that's a littel inconvenient. "liyu" <yunwind@msn.com> wrote in message news:gnob29$1hcd$1@digitalmars.com... > 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); } > } | |||
February 21, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to liyu | liyu wrote:
> maybe i should title it assert or execption can't terminate the prog in
> multithread:-)
> yes, the main thread is still running, but i want the program terminated
> when a assert failure or exception happened whatever. otherwise i have
> to notify the main thread, i think that's a littel inconvenient.
You could make a feature request for Tango. That's where the issue lies.
| |||
February 21, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Christopher Wright | On Sat, Feb 21, 2009 at 9:04 AM, Christopher Wright <dhasenan@gmail.com> wrote:
> liyu wrote:
>> maybe i should title it assert or execption can't terminate the prog in
>> multithread:-)
>> yes, the main thread is still running, but i want the program terminated
>> when a assert failure or exception happened whatever. otherwise i have
>> to notify the main thread, i think that's a littel inconvenient.
>
> You could make a feature request for Tango. That's where the issue lies.
It might not even be necessary. tango.core.Exception has a way to replace the default assertion handler so that you can i.e. stop all threads when an assertion fails.
| |||
February 22, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | yup, that's what i need, thanks all for the reply! "Jarrett Billingsley" <jarrett.billingsley@gmail.com> wrote in message news:mailman.813.1235235089.22690.digitalmars-d@puremagic.com... > On Sat, Feb 21, 2009 at 9:04 AM, Christopher Wright <dhasenan@gmail.com> wrote: >> liyu wrote: >>> maybe i should title it assert or execption can't terminate the prog in >>> multithread:-) >>> yes, the main thread is still running, but i want the program terminated >>> when a assert failure or exception happened whatever. otherwise i have >>> to notify the main thread, i think that's a littel inconvenient. >> >> You could make a feature request for Tango. That's where the issue lies. > > It might not even be necessary. tango.core.Exception has a way to > replace the default assertion handler so that you can i.e. stop all > threads when an assertion fails. | |||
February 22, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to liyu | On Sat, Feb 21, 2009 at 7:54 PM, liyu <yunwind@msn.com> wrote:
> yup, that's what i need, thanks all for the reply!
Yay!
| |||
February 24, 2009 Re: assert or execption case program hang in multi thread | ||||
|---|---|---|---|---|
| ||||
Posted in reply to liyu | liyu 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); } > } you should write thread.join() instead of Thread.sleep(100); the syntax might be incorrect. search "join" in the thread class in tango. have a nice day ^^) -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply