CCriticalSection

  • Thread starter Thread starter David Hardek
  • Start date Start date
D

David Hardek

Does CCriticalSection work in VC++.NET.

I have code that uses CCriticalSection 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.

CCriticalSection criticalSection;
CSingleLock singleLock(&criticalSection)
singleLock.Lock(1000); // This call generates the
exception - doesn't look like criticalSection is getting
created correctly
singleLock.Unlock();
 
I have code that uses CCriticalSection 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.

Instead of:
singleLock.Lock(1000);

use:

singleLock.Lock();

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

"Note CCriticalSection, 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
 
The .Lock method is on the CSingleLock object which does
take a timeout value (which we want to have), the .Lock on
the CCriticalSection object generates and exception also.

One other note is that if I change CCriticalSection to
CMutex it works, which I realize is a viable solution, I
was just under the impression that CCriticalSection would
be more efficient.
 
From the MSDN online help

To access a resource controlled by a CCriticalSection
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::Lock). 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::Unlock), or allow the lock
object to fall out of scope.

I agree on the CCriticalSection object you cannot use a
wait timeout, but if you are using CSingleLock in
conjunction with the CCriticalSection 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 CCriticalSection, not
using the CSingleLock object I get the same result, an
assertion.

If I do the following

CCriticalSection 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.
 
From the MSDN online help
To access a resource controlled by a CCriticalSection
object in this manner, first create a variable of type
CSingleLock in your resource's access member function.
...

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

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, EnterCriticalSection 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
 
Back
Top