| title | CCriticalSection Class | Microsoft Docs | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ms.custom | ||||||||||||||
| ms.date | 11/04/2016 | |||||||||||||
| ms.reviewer | ||||||||||||||
| ms.suite | ||||||||||||||
| ms.technology |
|
|||||||||||||
| ms.tgt_pltfrm | ||||||||||||||
| ms.topic | reference | |||||||||||||
| f1_keywords |
|
|||||||||||||
| dev_langs |
|
|||||||||||||
| helpviewer_keywords |
|
|||||||||||||
| ms.assetid | f776f74b-5b0b-4f32-9c13-2b8e4a0d7b2b | |||||||||||||
| caps.latest.revision | 21 | |||||||||||||
| author | mikeblome | |||||||||||||
| ms.author | mblome | |||||||||||||
| manager | ghogen | |||||||||||||
| translation.priority.ht |
|
Represents a "critical section" — a synchronization object that allows one thread at a time to access a resource or section of code.
class CCriticalSection : public CSyncObject
| Name | Description |
|---|---|
| CCriticalSection::CCriticalSection | Constructs a CCriticalSection object. |
| Name | Description |
|---|---|
| CCriticalSection::Lock | Use to gain access to the CCriticalSection object. |
| CCriticalSection::Unlock | Releases the CCriticalSection object. |
| Name | Description |
|---|---|
| CCriticalSection::operator CRITICAL_SECTION* | Retrieves a pointer to the internal CRITICAL_SECTION object. |
| Name | Description |
|---|---|
| CCriticalSection::m_sect | A CRITICAL_SECTION object. |
Critical sections are useful when only one thread at a time can be allowed to modify data or some other controlled resource. For example, adding nodes to a linked list is a process that should only be allowed by one thread at a time. By using a CCriticalSection object to control the linked list, only one thread at a time can gain access to the list.
Note
The functionality of the CCriticalSection class is provided by an actual Win32 CRITICAL_SECTION object.
Critical sections are used instead of mutexes (see CMutex) when speed is critical and the resource will not be used across process boundaries.
There are two methods for using a CCriticalSection object: stand-alone and embedded in a class.
-
Stand-alone method To use a stand-alone
CCriticalSectionobject, construct theCCriticalSectionobject when it is needed. After a successful return from the constructor, explicitly lock the object with a call to Lock. Call Unlock when you are done accessing the critical section. This method, while clearer to someone reading your source code, is more prone to error as you must remember to lock and unlock the critical section before and after access.A more preferable method is to use the CSingleLock class. It also has a
LockandUnlockmethod, but you don't have to worry about unlocking the resource if an exception occurs. -
Embedded method You can also share a class with multiple threads by adding a
CCriticalSection-type data member to the class and locking the data member when needed.
For more information on using CCriticalSection objects, see the article Multithreading: How to Use the Synchronization Classes.
CCriticalSection
Header: afxmt.h
Constructs a CCriticalSection object.
CCriticalSection();
To access or release a CCriticalSection object, create a CSingleLock object and call its Lock and Unlock member functions. If the CCriticalSection object is being used stand-alone, call its Unlock member function to release it.
If the constructor fails to allocate the required system memory, a memory exception (of type CMemoryException) is automatically thrown.
See the example for CCriticalSection::Lock.
Call this member function to gain access to the critical section object.
BOOL Lock();
BOOL Lock(DWORD dwTimeout);
dwTimeout
Lock ignores this parameter value.
Nonzero if the function was successful; otherwise 0.
Lock is a blocking call that will not return until the critical section object is signaled (becomes available).
If timed waits are necessary, you can use a CMutex object instead of a CCriticalSection object.
If Lock fails to allocate the necessary system memory, a memory exception (of type CMemoryException) is automatically thrown.
This example demonstrates the nested critical section approach by controlling access to a shared resource (the static _strShared object) using a shared CCriticalSection object. The SomeMethod function demonstrates updating a shared resource in a safe manner.
[!code-cppNVC_MFC_Utilities#11]
Contains a critical section object that is used by all CCriticalSection methods.
CRITICAL_SECTION m_sect;
Retrieves a CRITICAL_SECTION object.
operator CRITICAL_SECTION*();
Call this function to retrieve a pointer to the internal CRITICAL_SECTION object.
Releases the CCriticalSection object for use by another thread.
BOOL Unlock();
Nonzero if the CCriticalSection object was owned by the thread and the release was successful; otherwise 0.
If the CCriticalSection is being used stand-alone, Unlock must be called immediately after completing use of the resource controlled by the critical section. If a CSingleLock object is being used, CCriticalSection::Unlock will be called by the lock object's Unlock member function.
See the example for CCriticalSection::Lock.