BeginCriticalRegion - EndCriticalRegion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Um9nZXI=?=

    BeginCriticalRegion - EndCriticalRegion

    Hi,

    As I understand, if a code section inside a sub called with threadX.Start
    is between BeginCriticalRe gion - EndCriticalRegi on, 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(AddressO f subWithASleep)
    myThreadx.Start ()
    Thread.Sleep(20 00) 'For the called thread to have some time to enter that
    "begin-critical-region"

    myThreadx.Abort ()
    Console.WriteLi ne("Main thread finished")

    end sub


    Public Sub subWithASleep()

    Thread.BeginCri ticalRegion()

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

    Thread.EndCriti calRegion()

    End Sub


    --
    Roger
    ..NET 2005 and DB developer
  • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

    #2
    RE: BeginCriticalRe gion - EndCriticalRegi on

    Are you confusing Critical Regions and Constrained Execution Regions?

    BeginCriticalRe gion/EndCriticalRegi on 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. BeginCriticalRe gion/EndCriticalRegi on 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]
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.



    --
    Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

    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 BeginCriticalRe gion - EndCriticalRegi on, 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(AddressO f subWithASleep)
    myThreadx.Start ()
    Thread.Sleep(20 00) 'For the called thread to have some time to enter that
    "begin-critical-region"
    >
    myThreadx.Abort ()
    Console.WriteLi ne("Main thread finished")
    >
    end sub
    >
    >
    Public Sub subWithASleep()
    >
    Thread.BeginCri ticalRegion()
    >
    Thread.Sleep(15 000) 'waits 15 seconds
    Console.Write(" I'm still standing, yeh yeh yéah") 'It should show
    but it does NOT !
    >
    Thread.EndCriti calRegion()
    >
    End Sub

    Comment

    • =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?=

      #3
      RE: BeginCriticalRe gion - EndCriticalRegi on

      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?
      >
      BeginCriticalRe gion/EndCriticalRegi on 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. BeginCriticalRe gion/EndCriticalRegi on 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]
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      >
      >
      --
      Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

      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 BeginCriticalRe gion - EndCriticalRegi on, 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(AddressO f subWithASleep)
      myThreadx.Start ()
      Thread.Sleep(20 00) 'For the called thread to have some time to enter that
      "begin-critical-region"

      myThreadx.Abort ()
      Console.WriteLi ne("Main thread finished")

      end sub


      Public Sub subWithASleep()

      Thread.BeginCri ticalRegion()

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

      Thread.EndCriti calRegion()

      End Sub
      >

      Comment

      Working...