Thread Synchronization Mechanism (zz)

Source: Internet
Author: User

Some thread synchronization mechanisms are used today, so the four synchronization mechanisms of reprint threads are as follows:

1. Event
Using events to synchronize threads is the most flexible. An event has two States: the triggered state and the unfired state. It is also known as having a signal or having no signal. There are two types of events: manual event resetting and automatic event resetting. After the manual reset event is set to the excitation status, it will wake up all the waiting threads and remain in the excitation statusProgramSet it to the not activated status again. When an automatic reset event is set to the excitation status, it will wake up the "one" Waiting thread and automatically restore it to the unfired status. Therefore, it is ideal to use an automatic reset event to synchronize two threads. The corresponding class in MFC is cevent .. By default, the constructor of cevent creates an event that is automatically reset and is not activated. There are three functions to change the event status: setevent, resetevent, and pulseevent. It is ideal to use events to synchronize threads. However, in actual use, calling setevent and pulseevent for automatic reset events may cause deadlocks, be careful.

2. Critical Section
The first advice to use a critical region is not to lock a resource for a long time. The length of time here is relative, depending on different programs. For some control software, it may be several milliseconds, but for some other programs, it can be up to several minutes. However, after entering the critical section, you must exit as soon as possible to release resources. What if it is not released? The answer is no. If the main thread (GUI thread) is to enter an unreleased critical section, the program will be suspended! A drawback of the critical zone is that the critical section is not a core object and cannot be known whether the thread entering the critical zone is dead or not. If the thread entering the critical zone fails, the critical resources are not released, the system cannot know and cannot release the critical resource. This disadvantage is compensated by mutex. The implementation class of the critical section in MFC is ccriticalsection. Ccriticalsection: Lock () enters the critical section, and ccriticalsection: Unlock () leaves the critical section.

3. mutex
The functions of the mutex are similar to those of the critical zone. The difference is: mutex takes more time than the critical section, but mutex is the core object (the event and semaphore are also) and can be used across processes, in addition, waiting for a locked mutex can set timeout, so that the critical area is not known as the critical section, and the system will always die. The corresponding class in MFC is cmutex. Win32 functions: Create a mutex createmutex (), open the mutex openmutex (), and release the mutex releasemutex (). Mutex's ownership does not belong to the thread that generates it, but the last thread that waits for the mutex (waitforsingleobject, etc.) and has not performed the releasemutex () operation. A thread has mutex, just like entering the critical section. Only one thread can own the mutex at a time. If a thread with a mutex does not call releasemutex () before returning, the mutex is discarded, but the mutex can still be returned when other threads wait for the mutex (waitforsingleobject, etc, and get a value of wait_abandoned_0. It can be seen that a mutex is exclusive to mutex.

4. semaphore
Semaphores are the most historical synchronization mechanism. Semaphores are the key elements to solve the producer/consumer problem. The corresponding MFC class is csemaphore. Win32 function createsemaphore () is used to generate semaphores. Releasesemaphore () is used to unlock the lock. The current value of semaphore indicates the number of currently available resources. If the current value of semaphore is 1, another lock action can be successful. If the present value is 5, five lock actions can be successful. When wait... If the current value of semaphore is not 0, wait... Return immediately. The number of resources is reduced by 1. When releasesemaphore () is called, the Resource Count Plus 1 will not exceed the initial set total number of resources.

I feel that the biggest failure of the Operating System Course in undergraduate courses is that we did not let us practice the concept of multithreading, synchronization mechanism, IPC, and so on ......

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.