Multithreaded (c + +) critical section critical Sections

Source: Internet
Author: User
Tags exit in mutex

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:

    1. Defines a global object for the CCriticalSection class (so that each thread can access it), such as CCriticalSection critical_section;
    2. 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.
    3. After the Access critical section is complete, use the member function of CCriticalSection unlock () to release the critical section:
      critical_section. Unlock ();
    4. 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.
    5. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.