473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 5035
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Ken Durden | last post by:
I already posted this to comp.programming.threads, but the group over there seems to be arguing from a purely theoretical standpoint rather than addressing the reality of compiler optimizations and...
2
by: Ben Taylor | last post by:
Hi, I am writing a program that uses different threads to handle various painting operations, I wondered if somebody could possible tell me if I create a new thread using...
5
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio...
1
by: diskDriveGuy | last post by:
I have a program that runs a thread for each device under test (144) the program communicates to the Device Under Test (DUT) thru the serial port. Originally the program only allowed 1 thread at a...
9
by: Arsalan Ahmad | last post by:
Hi all, I have developed a static library which I am using in one of my application. In my library I have created my own heap and all the objects (class objects) in my application are created in...
3
by: freesteel | last post by:
/* Is this a bug in Py_NewInterpreter? The function below "MyThread" is instantiated from a windows worker thread, but I don't think that is relevant. (I can try this on a linux box, but I...
1
by: Martin.C.Johnsson | last post by:
Hi, I have the following problem The below class is fully defined in the header file thus there's no ..cpp-file. The header-file is included in several lib's I'm using. ---------- #pragma once...
1
by: werp | last post by:
I am using CCriticalSection and CSingleLock in an MFC app. I have created an object and pass into it a CCriticalSection from the main app. Then I make a thread. The thread constantly writes data to...
3
by: Donos | last post by:
I have an app in which there are 4 threads running simultaneously. Right now am using "Events" to do the synchronization between threads. Like, there is a Server writing thread and then there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.