First, Critical Sections (the same function as the mutex, to ensure that only one thread at a time can access the shared resources, but is not a kernel object, so access faster than the mutex, but there is no waiting for the timeout function, it is possible to cause deadlocks, use to be careful)
You can use the critical section object when multiple threads access an exclusive shared resource. At any one time only one thread can have a critical section object, a thread with a critical section can access the protected resource or code snippet, and other threads wishing to enter the critical section will be suspended until the critical section is discarded by the thread that owns the critical section, ensuring that multiple threads are not accessing the shared resource at the same time.
The CCriticalSection class is very simple to use, with the following steps:
- Defines a global object for the CCriticalSection class (so that each thread can access it), such as CCriticalSection critical_section;
- Before accessing the resource or code to be protected, call the member of the CCriticalSection Class lock () to obtain the critical section object:
critical_section. Lock ();
The function is called in the thread to get the critical section it requested. If no other thread occupies a critical section object at this time, the thread that calls lock () obtains a critical section, otherwise the thread is suspended and placed in a system queue until the thread that currently owns the critical section releases the critical section.
- After the Access critical section is complete, use the member function of CCriticalSection unlock () to release the critical section:
critical_section. Unlock ();
In a more popular sense, thread a executes to critical_section. Lock (); statement, if other thread (B) is executing critical_section. Lock (); statement and critical_section. Unlock (); The statement before the statement, thread a waits until thread B finishes executing critical_section. Unlock (); statement, thread A will continue execution.
- Precautions
1) Because critical sections is not a kernel object, it can only be used to synchronize synchronization between threads of a process, and cannot be used for synchronization between multiple different processes.
2) If the program crash or exit in the middle of the critical sections does not call LeaveCriticalSection, the result is that the kernel corresponding to the thread cannot be freed, and that it becomes a dead thread.
3) faster than other kernel objects.
Second, using a simple example of criticalsections, stack in the push can be divided into 3 steps, see the following code, but if after the 2nd step after this thread break switch to another thread, the other thread push and then return to execution, this thread continues to execute, It is possible that the other thread push will be overwritten and cannot be found in the stack. (The following code may have a problem with criticalsection,release under Debug)
Multithreaded (c + +) critical section critical Sections