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, we can enhance our understanding of the lock, and on the other hand, we can improve the performance and robustness of the Code. 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,

    InitializeCriticalSection    EnterCriticalSection    LeaveCriticalSection    DeleteCriticalSection

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

    EnterCriticalSection(/*...*/)        do_something();    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:

    CreateMutex    OpenMutex    ReleaseMutex

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

    WaitForSingleObject(/*...*/);        do_something();    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,

    CreateSemaphore    OpenSemaphore    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,

    WaitForSingleObject(/*...*/);        do_something();    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, the main thread often needs 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,

    CreateEvent    OpenEvent    PulseEvent    ResetEvent    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 creating an event, you must consider the initial status and basic attributes of the event.
This should be done for main thread,

    CreateEvent(/*...*/);    SetEvent(/*...*/);    WaitForMultiObjects(hThread, /*...*/);    CloseHandle(/*...*/);

For normal thread, the operation is relatively simple,

    while(1){        WaitForSingleObject(/*...*/);        /*...*/    }

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.

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.