[Windows] [thread] synchronization. Non-kernel. criticalsection

Source: Internet
Author: User
Overview: A simple way to ensure that only one thread can access data at a certain time. Only one thread is allowed to access Shared resources at any time. If multiple threads attempt to access the critical section at the same time, all other threads attempting to access the critical section will be suspended and will continue until the thread enters the critical section. After the critical section is released, other threads can continue to seize it and use the atomic method to share resources.

Critical section:Among all synchronization objects, the critical section is the easiest to use, but it can only be used to synchronize threads in a single process. Only one thread is allowed to access the critical area of a Data zone at a time. In addition, among these synchronization objects, only the critical section is not a kernel object. It cannot be managed by low-level components of the operating system, and cannot be operated by a handle. Because it is not a kernel object, as a lightweight synchronization mechanism, the synchronization speed is faster.

Procedure:

 

1. Create a critical section in the process, that is, allocate a critical_section data structure in the process. The distribution of the structure of the critical section must be global, so that different threads of the process can access it. For an in-depth analysis of the critical_section struct, refer to the article:

<Break free of code deadlocks in critical sections under Windows>

2. Before using a thread for synchronization in the critical section, you must call initializecriticalsection to initialize the critical section. Before releasing a resource, you only need to initialize it once.

3. Void entercriticalsection: blocking function. The function returns when the calling thread is granted ownership. In other words, when the calling thread cannot obtain the ownership of the specified critical section, the thread will sleep and the system will not allocate CPU to it before it is awakened. Alternatively, you can use the tryentercriticalsection method to access the critical section. If the entry is successful, the caller's thread obtains the right to use the critical section. Otherwise, the return result fails.

4. execute tasks in the critical section

5. bool leavecriticalsection: non-blocking function. Subtract one from the reference count of the current thread for the specified critical section. When the count is set to zero, the other thread waits for the reference count of this critical section.OneThe thread will be awakened.

6. When you do not need to use this critical section, use deletecriticalsection to release the resources required in the critical section. After this function is executed, entercriticalsection and leavecriticalsection cannot be used any more, unless the critical section is initialized again using initializecriticalsection.

  Note:

1. only one thread is allowed to access the critical area at a time. Each thread must call the critical area mark (that is, a critical_section global variable) after the entercriticalsection before the view operates the critical area data, other threads that want to gain access are put in sleep state, and before being awakened, the system stops allocating CPU time slices to them. In other words, the critical section can be owned by only one thread. Of course, when no thread calls entercriticalsection or tryentercriticalsection, the critical section does not belong to any thread.

2. When a thread with the critical zone ownership calls leavecriticalsection to discard the ownership, the system only wakes up one thread in the waiting queue and gives it the ownership. Other threads continue to wait.

3. note that each call to the entercriticalsection of a thread in this critical section is successful (repeated calls are returned immediately, that is, nested calls are supported ), the reference count of the critical section mark (A critical_section global variable) is increased by 1. Before another thread can have this critical section, the thread that owns it must call leavecriticalsection multiple times. Only when the reference count is reduced to zero can the other thread have this critical section. In other words, in a thread that normally uses the critical section, the calsection and leavecriticalsection shouldPaired use.

4. tryentercriticalsection
Bool tryentercriticalsection (maid section );
From the function declaration, we can see that the return value of the entercriticalsection function is void, and here it is bool. It can be seen that for calls to tryentercriticalsection, we need to determine the return value. When tryentercriticalsection is called, if the specified critical section is not owned by any thread (or has not been owned by any call thread), the function gives the access permission of the critical section to the calling thread and returns true; however, if the critical section is already owned by another thread, it immediately returns false. The biggest difference between tryentercriticalsection and entercriticalsection is that tryentercriticalsection never suspends a thread.

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: info-contact@alibabacloud.com 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.