BeginCriticalRegion - EndCriticalRegion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

As I understand, if a code section inside a sub called with threadX.Start
is between BeginCriticalRegion - EndCriticalRegion, it should complete (at
least that portion of code, not the whole 'sub'... even if a TreadX.Abort is
used, if this portion of code is running when you call that Abort.

So With this program, I would expect to print :

I'm still standing, yeh yeh yéah
Main thread finished

But only

Main thread finished

appears because thread is aborted although The code between critical region
is running... Why ?

Code:


Sub Main()


Dim myThreadx As New Thread(AddressOf subWithASleep)
myThreadx.Start()
Thread.Sleep(2000) 'For the called thread to have some time to enter that
"begin-critical-region"

myThreadx.Abort()
Console.WriteLine("Main thread finished")

end sub


Public Sub subWithASleep()

Thread.BeginCriticalRegion()

Thread.Sleep(15000) 'waits 15 seconds
Console.Write("I'm still standing, yeh yeh yéah") 'It should show
but it does NOT !

Thread.EndCriticalRegion()

End Sub
 
Are you confusing Critical Regions and Constrained Execution Regions?

BeginCriticalRegion/EndCriticalRegion is used to signify a critical region
of code--a region of code that non-atomically modifies data shared with other
threads. Interruption of code in critical region of code generally would
affect other threads. BeginCriticalRegion/EndCriticalRegion don't stop code
from being interrupted; it allows the host to decide what to do if a critical
region does get interrupted (like unload the AppDomain the code is in).

Manually specifying critical regions when using Monitor (and hence the
lock/SyncLock keywords) or Mutex appears largely redundant as Monitor and
Mutex already do this (as described by Jeffery Richter [1]; although I can
find no Microsoft documentation on this).

Are you trying to write a block of code that can't be interrupted?

[1]
http://msdn.microsoft.com/msdnmag/issues/06/03/concurrentaffairs/default.aspx
 
Thanks, Peter. I'll take a deeper look at this.

Bye,

--
Roger Tranchez
..NET 2005 and DB developer


Peter Ritchie said:
Are you confusing Critical Regions and Constrained Execution Regions?

BeginCriticalRegion/EndCriticalRegion is used to signify a critical region
of code--a region of code that non-atomically modifies data shared with other
threads. Interruption of code in critical region of code generally would
affect other threads. BeginCriticalRegion/EndCriticalRegion don't stop code
from being interrupted; it allows the host to decide what to do if a critical
region does get interrupted (like unload the AppDomain the code is in).

Manually specifying critical regions when using Monitor (and hence the
lock/SyncLock keywords) or Mutex appears largely redundant as Monitor and
Mutex already do this (as described by Jeffery Richter [1]; although I can
find no Microsoft documentation on this).

Are you trying to write a block of code that can't be interrupted?

[1]
http://msdn.microsoft.com/msdnmag/issues/06/03/concurrentaffairs/default.aspx


--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


Roger said:
Hi,

As I understand, if a code section inside a sub called with threadX.Start
is between BeginCriticalRegion - EndCriticalRegion, it should complete (at
least that portion of code, not the whole 'sub'... even if a TreadX.Abort is
used, if this portion of code is running when you call that Abort.

So With this program, I would expect to print :

I'm still standing, yeh yeh yéah
Main thread finished

But only

Main thread finished

appears because thread is aborted although The code between critical region
is running... Why ?

Code:


Sub Main()


Dim myThreadx As New Thread(AddressOf subWithASleep)
myThreadx.Start()
Thread.Sleep(2000) 'For the called thread to have some time to enter that
"begin-critical-region"

myThreadx.Abort()
Console.WriteLine("Main thread finished")

end sub


Public Sub subWithASleep()

Thread.BeginCriticalRegion()

Thread.Sleep(15000) 'waits 15 seconds
Console.Write("I'm still standing, yeh yeh yéah") 'It should show
but it does NOT !

Thread.EndCriticalRegion()

End Sub
 
Back
Top