On Thursday, 4 February 2016 at 22:27:31 UTC, Ali Çehreli wrote:
On 02/04/2016 12:25 PM, tcak wrote:

> void threadFunc(){
>      scope(exit){
>          writeln("Leaving 2: ", stopRequested);
>      }
>
>
>      while( !stopRequested ){
> /* THERE IS NO "RETURN" HERE AT ALL */
>      }
>
>      writeln("Leaving 1: ", stopRequested);
> }
>
>
>
> While loop is running, suddenly "Leaving 2: false" is seen.

That would happen when there is an exception.

> Checked with
> exception, but there is nothing.

If a thread is terminated with an exception, its stack is unwound and unlike the main thread, the program will not terminate. I think this is due to an exception.

> GDB doesn't show any error.

I think putting a break point at exception construction would be helpful but it will be simpler to put a try-catch block that covers the entire body of threadFunc().

> There is no
> "Leaving 1: .." message at all.
>
> Is there any known reason for a thread to suddenly stop like
this?

I am still betting on an exception. :)

Ali

Yup, it is exception it seems like, but with a weird result. Check the new codes:

void threadFunc(){
        scope(exit){
                writeln("Leaving 2: ", stopRequested);
        }

        scope(failure){
                writeln("Failure");
        }

        try{
                while( !stopRequested ){

                }

                writeln("Leaving 1: ", stopRequested);
        }
        catch( Exception ex ){
                writeln("Caught the exception");
        }
}

Now, the thread stops with:

Failure
Leaving 2: false


There is no "Caught the exception". And believe me other then the codes inside while loop, main structure as seen in the above code.

By testing many times, I understood that the problem occurs when too many requests are received suddenly (by pressing F5 many times again and again produces the exception).

But the question is why try-catch is not able to catch it, and just scope(failure) can?

Reply via email to