Multi-threaded things 06 (Windows Lock)

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

In Windows, the system itself provides us with many locks. Through the use of these locks, on the one hand, we can enhance our understanding of the lock, on the other hand, we can improveCodePerformance and robustness. The following four types of locks are commonly used: critical section, mutex, semaphore, and event.

(1) critical section

The critical section is the simplest lock. Basic critical section operations include,

[CPP] View plaincopy
    1. Initializecriticalsection
    2. Entercriticalsection
    3. Leavecriticalsection
    4. Deletecriticalsection

If you want to perform mutually exclusive operations on the data, it is also very easy to do so,

[CPP] View plaincopy
    1. Entercriticalsection (/*...*/)
    2. Do_something ();
    3. Leavecriticalsection (/*...*/)

(2) mutex lock
Mutex lock is also a type of lock. Different from the critical section, it can be used by different processes because it has a name. At the same time, the thread for obtaining and Releasing locks must be the same thread. Common mutex lock operations include:

[CPP] View plaincopy
    1. Createmutex
    2. Openmutex
    3. Releasemutex

Therefore, it is not difficult to use mutex to access data.

[CPP] View plaincopy
    1. Waitforsingleobject (/*...*/);
    2. Do_something ();
    3. Releasemutex (/*...*/);

(3) semaphores
Semaphores are the most commonly used lock results and the most convenient one. Around semaphores, people have proposed a lot of mutually exclusive data access solutions. PV operations are one of them. If mutex locks can only protect a single resource, semaphores can protect multiple resources. At the same time, when the semaphore is unlocked, it can be unlocked by another thread. Currently, common semaphore operations include,

[CPP] View plaincopy
    1. Createsemaphore
    2. Opensemaphore
    3. Releasesemaphore

The use of semaphores is similar to that of mutex locks. The key is that when the semaphore is initialized, it is necessary to determine the quantity of the current resource and the initial state of the semaphore,

[CPP] View plaincopy
    1. Waitforsingleobject (/*...*/);
    2. Do_something ();
    3. Releasesemaphore (/*...*/);

(4) event object
The event object is an interesting lock result in windows. In a sense, it is very similar to the mutex lock, but not the same. Because before the thread obtains the right to use the lock, it is often necessary for the main thread to call setevent to set a lock. The key is that before the thread ends, we do not know where to execute the event after the current thread obtains the event. So be careful when using it. Common event operations include,

[CPP] View plaincopy
    1. Createevent
    2. Openevent
    3. Pulseevent
    4. Resetevent
    5. Setevent

We are used to dividing the use of events into main thread and normal thread. Main thread is responsible for event settings and operations, while normal thread is responsible for event wait operations. When createevent is created, you must understand the initial status and basic attributes of the event.
This should be done for main thread,

[CPP] View plaincopy
    1. Createevent (/*...*/);
    2. Setevent (/*...*/);
    3. Waitformultiobjects (hthread,/*...*/);
    4. Closehandle (/*...*/);

For normal thread, the operation is relatively simple,

[CPP] View plaincopy
    1. While(1 ){
    2. Waitforsingleobject (/*...*/);
    3. /*...*/
    4. }

Summary:
(1) sample code is available on msdn for critical section, mutex, semaphore, and event.
(2) Generally, semaphores> mutex> critical section> event objects are used in frequency.
(3) semaphores can implement the other three locks, so learning should focus on
(4) The difference between them can be grasped only when there are more practices.

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: 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.