PC Review


Reply
Thread Tools Rate Thread

BeginCriticalRegion - EndCriticalRegion

 
 
=?Utf-8?B?Um9nZXI=?=
Guest
Posts: n/a
 
      14th Apr 2007
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


--
Roger
..NET 2005 and DB developer
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=
Guest
Posts: n/a
 
      15th Apr 2007
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/is...s/default.aspx


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


"Roger" wrote:

> 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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?=
Guest
Posts: n/a
 
      28th May 2007
Thanks, Peter. I'll take a deeper look at this.

Bye,

--
Roger Tranchez
..NET 2005 and DB developer


"Peter Ritchie [C# MVP]" wrote:

> 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/is...s/default.aspx
>
>
> --
> Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
> http://www.peterRitchie.com/blog/
> Microsoft MVP, Visual Developer - Visual C#
>
>
> "Roger" wrote:
>
> > 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

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't really understand BeginCriticalRegion/EndCriticalRegion Tony Johansson Microsoft C# .NET 1 16th Jun 2010 09:30 PM
Begin/EndCriticalRegion Moshe Peleg Microsoft Dot NET Compact Framework 7 9th Jul 2007 10:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:49 PM.