Home >Common Problem >What does multi-thread synchronization mechanism include?

What does multi-thread synchronization mechanism include?

coldplay.xixi
coldplay.xixiOriginal
2021-01-07 15:24:197641browse

Multi-thread synchronization mechanism includes: 1. Critical Section (critical section), used to achieve "exclusive possession"; 2. Semaphore, used to track limited resources; 3. Mutex, which can be used in different threads To achieve "exclusive possession" between threads, even if those threads belong to different processes; 4. Event, usually used for overlapped I/O, or used to design some custom synchronization objects.

What does multi-thread synchronization mechanism include?

#The operating environment of this article: Windows 7 system, Dell G3 computer.

Multi-thread synchronization mechanism includes:

1, Critical Section

Critical section (critical section) is used to implement " exclusive possession”. The scope of application is between threads of a single process. It is:

  • A local object, not a core object.

  • Fast and efficient.

  • More than one critical section cannot be waited for at the same time.

  • Unable to detect whether it has been abandoned by a thread.

  • can achieve mutual exclusion between threads, but cannot be used to achieve synchronization.

2, Semaphore

Semaphore is used to track limited resources. It is:

  • A core object.

  • No owner.

  • can be named, so it can be opened by other processes.

  • Can be released by any thread.

  • It can realize mutual exclusion between threads and synchronization between threads.

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

3, Mutex

Mutex is a core object that can achieve "exclusive possession" between different threads, even those threads Belong to different processes. It is:

  • A core object.

  • If the thread that owns the mutex terminates, an "abandoned" error message will be generated.

  • i can be named and therefore can be opened by other processes.

  • can only be released by the thread that owns it.

  • When used across processes, if the process owning the mutex ends unexpectedly, other processes will receive a WAIT_ABANDOEND message.

4, Event

Event object is usually used for overlapped I/O, or to design some custom synchronization objects. It is:

  • A core object.

  • is used to achieve mutual exclusion and synchronization of threads.

  • can be named, so it can be opened by other processes.

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

Note: Critical sections and mutexes both have the concept of "thread ownership", so they cannot be used to achieve synchronization between threads, but can only be used to implement mutual exclusion. The reason is that the thread that creates the critical section or mutex can unconditionally enter the protected program section without waiting for LeaveCriticalSection(), SetEvent(), because it has this right. In addition, mutexes can handle "abandonment" operations very well. If the thread terminates unexpectedly without releasing the object, other threads can wait for a WAIT_ABANDONED_0. But neither events nor semaphores can do that.

Events and semaphores can realize mutual exclusion and synchronization between threads and processes.

In terms of usage efficiency, the critical section has the highest efficiency, because it is not a kernel object, and the other three are core objects. They need to be implemented with the help of the operating system, so the efficiency is relatively low. .

But if you want to use it across processes, you still need to use mutexes, event objects and semaphores.

In short: When designing, first try not to use global variables. If not, then consider using the Inter...() function, then the critical section object, and finally the event, mutex, signal.

The above is the detailed content of What does multi-thread synchronization mechanism include?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is softwareNext article:what is software