开发者

c# barrier and exception handeling

开发者 https://www.devze.com 2023-03-22 15:11 出处:网络
I have created simple c# example which uses barriers and I have thr开发者_开发百科own an exception inside one of functions and I\'m getting unexpected result

I have created simple c# example which uses barriers and I have thr开发者_开发百科own an exception inside one of functions and I'm getting unexpected result

phase 1 of DoWork1
phase 2 of DoWork2
phase 3 of DoWork3
Current phase 0
phase 2 of DoWork3
phase 2 of DoWork1
phase 3 of DoWork2
//so far everything is fine, lets throw an exception now

DoWork1 canceled
phase3 of DoWorn1

//end 

now as You can see in Code I have thrown an exception in DoWork1 method and I expected that all 3 method will handle exception, but only first one does, second problem is that only first method prints "phase 3 bla bla" and I expected all 3 of them to print it to console. Could Someone explain me why does this happen

Code is a bit long but most of it is just copy paste


First, an exception thrown on one thread will not generally be available on any other threads. Exceptions travel up the call stack, and each thread has its own stack.

Second, the reason the other two methods never reach phase 3 is that when DoWork1 throws the exception, it's because it tried to wait for the other participants in this barrier and failed. At that point, the barrier no longer believes that DoWork1 is waiting for the other participants, so when the other two SignalAndWait(), they wait forever, because DoWork1 never SignalAndWaits again.


Each thread has its own call stack. so only the thread in which you are throwing the exception the exception will be caught.

0

精彩评论

暂无评论...
验证码 换一张
取 消