469,332 Members | 6,650 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

CCriticalSection

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();

Nov 16 '05 #1
4 4625
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 16 '05 #2
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.
-----Original Message-----
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();

.

Nov 16 '05 #3
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.
-----Original Message-----
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

SynchronizationClasses" notes:

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

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

Nov 16 '05 #4
>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
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Ken Durden | last post: by
2 posts views Thread by Ben Taylor | last post: by
5 posts views Thread by Ron Louzon | last post: by
1 post views Thread by diskDriveGuy | last post: by
9 posts views Thread by Arsalan Ahmad | last post: by
1 post views Thread by Martin.C.Johnsson | last post: by
3 posts views Thread by Donos | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.