CCriticalSection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Hardek

    #1

    CCriticalSection

    Does CCriticalSectio n work in VC++.NET.

    I have code that uses CCriticalSectio n that worked in VC++
    6.0 and when I try and run it in VC++.NET I run into an
    exception error when I try and lock the critical section.

    Just as a note I tried the CMutex sample program and it
    works, but if I add in the following lines to that sample
    program I get the same error I see in my application.

    CCriticalSectio n criticalSection ;
    CSingleLock singleLock(&cri ticalSection)
    singleLock.Lock (1000); // This call generates the
    exception - doesn't look like criticalSection is getting
    created correctly
    singleLock.Unlo ck();

  • David Lowndes

    #2
    Re: CCriticalSectio n

    >I have code that uses CCriticalSectio n that worked in VC++[color=blue]
    >6.0 and when I try and run it in VC++.NET I run into an
    >exception error when I try and lock the critical section.[/color]

    Instead of:
    [color=blue]
    >singleLock.Loc k(1000);[/color]

    use:

    singleLock.Lock ();

    The MSDN topic titled "Multithreading : How to Use the Synchronization
    Classes" notes:

    "Note CCriticalSectio n, unlike other MFC synchronization classes,
    does not have the option of a timed lock request. The waiting period
    for a thread to become free is infinite.
    "

    Dave
    --
    MVP VC++ FAQ: http://www.mvps.org/vcfaq

    Comment

    • Guest's Avatar

      #3
      CCriticalSectio n

      The .Lock method is on the CSingleLock object which does
      take a timeout value (which we want to have), the .Lock on
      the CCriticalSectio n object generates and exception also.

      One other note is that if I change CCriticalSectio n to
      CMutex it works, which I realize is a viable solution, I
      was just under the impression that CCriticalSectio n would
      be more efficient.
      [color=blue]
      >-----Original Message-----
      >Does CCriticalSectio n work in VC++.NET.
      >
      >I have code that uses CCriticalSectio n that worked in[/color]
      VC++[color=blue]
      >6.0 and when I try and run it in VC++.NET I run into an
      >exception error when I try and lock the critical section.
      >
      >Just as a note I tried the CMutex sample program and it
      >works, but if I add in the following lines to that sample
      >program I get the same error I see in my application.
      >
      >CCriticalSecti on criticalSection ;
      >CSingleLock singleLock(&cri ticalSection)
      >singleLock.Loc k(1000); // This call generates the
      >exception - doesn't look like criticalSection is getting
      >created correctly
      >singleLock.Unl ock();
      >
      >.
      >[/color]

      Comment

      • Guest's Avatar

        #4
        Re: CCriticalSectio n

        From the MSDN online help

        To access a resource controlled by a CCriticalSectio n
        object in this manner, first create a variable of type
        CSingleLock in your resource's access member function.
        Then call the lock object's Lock member function (for
        example, CSingleLock::Lo ck). At this point, your thread
        will either gain access to the resource, wait for the
        resource to be released and gain access, or wait for the
        resource to be released and time out, failing to gain
        access to the resource. In any case, your resource has
        been accessed in a thread-safe manner. To release the
        resource, use the lock object's Unlock member function
        (for example, CSingleLock::Un lock), or allow the lock
        object to fall out of scope.

        I agree on the CCriticalSectio n object you cannot use a
        wait timeout, but if you are using CSingleLock in
        conjunction with the CCriticalSectio n you should be able
        to do a wait on the lock, this seems to work in VC++ 6.0.


        Even if I do the .Lock() on the CCriticalSectio n, not
        using the CSingleLock object I get the same result, an
        assertion.

        If I do the following

        CCriticalSectio n criticalSection ;
        int ii = 0;

        in the debugger when I get to the int ii=0 line,
        criticalSection is marked as undefined in the watch
        window, it looks like it never initializes for some
        reason, I think that is the root of my problem, I just
        can't figure out what I am not including or referencing to
        get this to work.


        [color=blue]
        >-----Original Message-----[color=green]
        >>I have code that uses CCriticalSectio n that worked in[/color][/color]
        VC++[color=blue][color=green]
        >>6.0 and when I try and run it in VC++.NET I run into an
        >>exception error when I try and lock the critical section.[/color]
        >
        >Instead of:
        >[color=green]
        >>singleLock.Lo ck(1000);[/color]
        >
        >use:
        >
        >singleLock.Loc k();
        >
        >The MSDN topic titled "Multithreading : How to Use the[/color]
        Synchronization[color=blue]
        >Classes" notes:
        >
        >"Note CCriticalSectio n, unlike other MFC[/color]
        synchronization classes,[color=blue]
        >does not have the option of a timed lock request. The[/color]
        waiting period[color=blue]
        >for a thread to become free is infinite.
        >"
        >
        >Dave
        >--
        >MVP VC++ FAQ: http://www.mvps.org/vcfaq
        >.
        >[/color]

        Comment

        • David Lowndes

          #5
          Re: CCriticalSectio n

          >From the MSDN online help[color=blue]
          >
          >To access a resource controlled by a CCriticalSectio n
          >object in this manner, first create a variable of type
          >CSingleLock in your resource's access member function.
          >...[/color]

          Got the URL for that? I can't find it in the current MSDN library.
          [color=blue]
          >I agree on the CCriticalSectio n object you cannot use a
          >wait timeout, but if you are using CSingleLock in
          >conjunction with the CCriticalSectio n you should be able
          >to do a wait on the lock, this seems to work in VC++ 6.0.[/color]

          I'm surprised this worked (or appeared to work) in VC6. As far as I
          know there's no timeout mechanism in the underlying critical section
          object. Here's what the SDK docs say:

          "... If the critical section object is currently owned by another
          thread, EnterCriticalSe ction waits indefinitely for ownership. In
          contrast, when a mutex object is used for mutual exclusion, the wait
          functions accept a specified time-out interval.
          "

          Dave
          --
          MVP VC++ FAQ: http://www.mvps.org/vcfaq

          Comment

          Working...