CCriticalSection Class |
Description
Critical sections are a feature of the Windows API. They are used to prevent a section of code from being executed by more than one thread at a time. Once a thread has locked a critical section, any other thread wishing to execute that code must wait until the critical section is released so it can lock the critical section.
For example, we might wish to prevent a situation where one thread is changing a global variable while another thread is using it. The CCriticalSection class provides a convenient and object orientated approach to using critical sections. It is used internally by the Win32++ library, but will also be useful to developers when coding their own multi-threaded applications.
Refer to the documentation that ships with the Microsoft Windows Software Development Kit for more information on the use of critical sections to assist with synchronisation in multi-threaded applications.
CCriticalSection Members
CCriticalSection | CCriticalSection();Constructor for CCriticalSection. |
Lock | void Lock();Enter a critical section. Only one thread at a time runs code protected by a critical section. |
Release | void Release();Leave a critical section. |
Remarks
The following example demonstrates the use of CCriticalSection.
// m_csVar is a class member variable of type CCriticalSection // Lock the critical section while we change its value m_csVar.Lock(); // Modify the global variable g_Var = 10; // Release the critical section m_csVar.Release(); // Note: we would also protect any reads from the global // variable with the same m_csVar CCriticalSection
Summary Information
Header file | wincore.h |
Win32/64 support | Yes |
WinCE support | Yes |