Several methods of multithreading synchronization mechanism

Source: Internet
Author: User
Tags mutex semaphore

Critical section
Critical section (critical section) is used to achieve "exclusive possession". Scope of application is a single process
Between the threads. It is:
A local object, not a core object.
Fast and efficient.
Not be able to have more than one critical section at the same time being waited.
Unable to detect whether a thread has been discarded.

You can implement mutual exclusion between threads and cannot be used to implement synchronization.

Semaphore
Semaphore is used to track limited resources. It is:
A core object.

No owner.

Can be named so that it can be opened by another process.
Can be freed by any one thread (released).

It can realize inter-thread mutual exclusion and synchronization between threads.

When used across processes, other processes do not receive notifications if the thread that owns the semaphore ends unexpectedly.

Mutex
A Mutex is a core object that can achieve "exclusive possession" between different threads,
Even if those threads belong to different processes. It is:
A core object.
If the thread that owns the mutex ends, it produces a "abandoned" error
Error messages.
I can be named so that it can be opened by other processes.
Can only be freed by the thread that owns it (released).

When used across processes, other processes receive a wait_abandoend message if the process that owns the mutex ends unexpectedly.


Event
The Event object is typically used for overlapped I/O, or for designing some custom
The synchronization object that is defined. It is:
A core object.
However, it is used to implement the mutex and synchronization of threads.
Can be named so that it can be opened by another process.

When used across processes, other processes will not be notified if the thread that owns the semaphore ends unexpectedly.

Note: Both the critical section and the mutex have the concept of "thread ownership", so they cannot be used for synchronization between threads and can only be used to implement mutual exclusion. The reason is that because a thread that creates a critical section or mutex can not wait for leavecriticalsection (), SetEvent () can enter the protected program section unconditionally because it has this right. In addition, the mutex can handle the "abandon" operation very well. If the thread terminates unexpectedly when the pair is not released, the other thread can wait for a wait_abandoned_0. But neither the event nor the semaphore can be done.

Both events and semaphores allow for mutual exclusion and synchronization between threads and processes.

As far as efficiency is concerned, the efficiency of the critical zone is the highest, because it is not a kernel object, while the other three are core objects, and it is relatively inefficient to use the operating system to achieve it.

However, if you are going to use the mutex, event object, and semaphore.

In short: At design time, try not to use global variables first, if you do not first then consider using Inter ... () function, and then in the critical section object, the last is the event, the mutex, the semaphore.

Several methods of multithreading synchronization mechanism

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.