Critical section
lastmod 2021-11-19created 2015-10-043 edits
A critical section (or shared variable region) in parallel computing is the part of code that accesses a shared resource (a data structure or device) that must not be accessed by more than one thread at the same time. A critical section terminates after a specified amount of time. Therefore, if a thread (task or process) wants to enter a critical section, it must wait for the specified time. A synchronization mechanism such as a semaphore is used when a thread enters or leaves a critical section, to guarantee exclusive use of the shared resource.
Comparison of Critical Section functions on Windows and Linux
A Critical Section is used on Windows to designate a critical region that multiple threads cannot access. On Windows, a Critical Section is used only as a user object; on Linux, you can implement it with a Mutex.
| Windows | Linux |
| InitializeCriticalSection() | pthread_mutex_init() |
| DeleteCriticalSection() | pthread_mutex_destroy() |
| EnterCriticalSection() | pthread_mutex_lock() |
| LeaveCriticalSection() | pthread_mutex_unlock() |
Win32 synchronization structures and functions
- CRITICAL_SECTION: Critical section synchronization structure.
- InitializeCriticalSection(): Initializes the critical section synchronization variable.
- DeleteCriticalSection(): Releases the critical section synchronization variable.
- EnterCriticalSection(): Takes ownership of the critical section synchronization variable. If another thread already owns it, enters the waiting state.
- TryEnterCriticalSection(): Takes ownership of the critical section synchronization variable. If another thread already owns it, returns immediately without entering the waiting state.
- LeaveCriticalSection(): Releases ownership of the critical section. Releases it if a thread already owns it.
How to use synchronization objects and functions
Windows
- Initialize with InitializeCriticalSection().
- EnterCriticalSection(), TryEnterCriticalSection(), and LeaveCriticalSection() can be used.
- Release with DeleteCriticalSection().
Linux
- Initialize the mutex (Mutex) with pthread_mutex_init().
- pthread_mutex_lock() and pthread_mutex_unlock() can be used.
- Release with pthread_mutex_destroy().
Simple Win32 C++ critical section class
To make a Linux version, replace only the corresponding functions and change the header file to pthread.h.
class Lock
{
private:
CRITICAL_SECTION _critical_section;
public:
Lock() {
InitializeCriticalSection(&_critical_section);
}
virtual ~Lock() {
DeleteCriticalSection(&_critical_section);
}
public:
void lock() {
EnterCriticalSection(&_critical_section);
}
void unlock() {
LeaveCriticalSection(&_critical_section);
}
};
See also
Favorite site
MSDN
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms682530(v=vs.85.aspx CRITICAL_SECTION Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms683472(v=vs.85.aspx InitializeCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms682552(v=vs.85.aspx DeleteCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608(v=vs.85.aspx EnterCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms686857(v=vs.85.aspx TryEnterCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms684169(v=vs.85.aspx LeaveCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms683472(v=vs.85.aspx InitializeCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms682608(v=vs.85.aspx EnterCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms686857(v=vs.85.aspx TryEnterCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms684169(v=vs.85.aspx LeaveCriticalSection() Reference]
- [http://msdn.microsoft.com/en-us/library/windows/desktop/ms682552(v=vs.85.aspx DeleteCriticalSection() Reference]