Second kill multithreading Article 9 typical thread synchronization summary key segment event mutex semaphore

Source: Internet
Author: User

In the previous article "second kill multi-thread 4: A typical multi-thread synchronization problem", a classic multi-thread synchronization mutex problem is raised. This problem includes synchronization between the main thread and the sub-thread, the mutex between sub-threads is a classic example of multi-thread synchronization mutex problem. Four articles are used later.

Second kill multi-thread Article 5 key segment of typical thread synchronization CS

Second kill multithreading Article 6 typical thread synchronization event

Second kill multithreading Article 7 synchronization mutex of classic threads

Second kill multithreading Article 8 typical thread synchronization semaphores semaphore

To introduce in detail the commonly used thread synchronization mutex mechanism-key segments, events, mutex volumes, semaphores. Next we will give a summary of them to help you sort out various knowledge points.

 

First of all, let's take a look at the conceptual knowledge about thread synchronization mutex. I believe you have some knowledge about thread synchronization mutex through the previous article, we can also blur the concepts of thread synchronization mutex. Next we will list some of the descriptions about thread synchronization mutex selected from the book computer operating system. I believe that we should have a preliminary and vague impression and look at the authoritative definition, which should be particularly memorable.

 

1. Main tasks of thread (process) Synchronization

A: After multithreading is introduced, the asynchronous thread execution will cause confusion to the system. Especially when critical resources are urgently used, for example, multiple threads urgently need the same printer, the printed results are intertwined and difficult to distinguish. Data processing errors may occur when multiple threads urgently use shared variables, tables, and linked lists, therefore, the main task of thread synchronization is to enable the threads for concurrent execution to effectively share resources and cooperate with each other, so that the program execution can be reproducible.

 

2. What are the constraints between threads (processes?

During Concurrent thread execution, there are two constraints between the threads used due to resource sharing and thread collaboration.

(1). Indirect mutual restraint. Multiple Threads in a system must share certain system resources, such as CPU sharing and I/O devices. The so-called indirect mutual restraint stems from the sharing of such resources. printer is the best example, when thread a is using a printer, other threads have to wait.

(2). Mutual restriction. This restriction is mainly because of the cooperation between threads. If thread a provides the computing result to thread B for further processing, thread B will be blocked before thread a delivers data.

Indirect mutual constraint can be calledMutual ExclusionAnd mutual restriction can be calledSynchronizationFor mutual exclusion, we can understand that when thread a and thread B access a resource at the same time, there will be a sequence problem between them-either thread a waits for thread B to complete the operation, either thread B waits for the thread to complete the operation, which is actually the synchronization of the thread. ThereforeSynchronization includes mutex. mutex is actually a special synchronization..

 

3. critical resources and Critical Zones

Resources that only allow access by one thread within a period of time are referred to as critical resources or exclusive resources. Most physical devices in the computer, and shared variable waits in the process are critical resources, they require mutex access. The code used to access critical resources in each process is called a critical zone.

 

After reading the conceptual knowledge, we will use several tables below to help you better remember and use the four implementation methods of multi-thread synchronization mutex-key segments, events, mutex volumes, and semaphores.

 

Key Section CS and mutex

 

Create or initialize

Destroy

Enter the mutex

Leave the mutex

Key Section CS

Initialize-

Criticalsection

Delete-

Criticalsection

Enter-

Criticalsection

Leave-

Criticalsection

Mutex

Createmutex

Closehandle

Wait for a series of functions such as waitforsingleobject

Releasemutex

Both the key section and mutex have the "thread ownership" concept, which can be understood as the "thread ownership" of the hotel room card, after registering a room card with the name at the hotel reception desk, you can enter the room multiple times. Others cannot enter the room until you hand in the room card. Each thread must first attempt to obtain "thread ownership" Through entercriticalsection or waitforsingleobject before calling leavecriticalsection or releasemutex. Otherwise, the call will fail, which is equivalent to forging the room card to go through the check-out procedure-because your name is not in the registration book, it will be rejected.

The mutex can well handle "abandon" situations, so it can be safely used between multiple processes.

 

Event

 

Create

Destroy

Trigger event

Make the event not triggered

Event

Createevent

Closehandle

Setevent

Resetevent

Note that the manual and automatic positions of the event should be clearly divided and should not be confused.

 

Semaphores semaphore

 

Create

Destroy

Decrease count

Incremental count

Semaphores

Semaphore

Create-

Semaphore

Closehandle

Wait for a series of functions such as waitforsingleobject

Release-

Semaphore

When the semaphore count is greater than 0, it indicates the trigger status. When waitforsingleobject is called, it will not be blocked. If it is equal to 0, it indicates the trigger status. When waitforsingleobject is called, it will be blocked until other threads increase the count.

 

Note: mutex, events, and semaphores are all kernel objects and can be used across processes (through openmutex, openevent, opensemaphore ). But why can't we solve the problem of "abandon" only by mutex? Please refer to "abandon" of second kill multi-thread Article 15th key segments, events, mutex volumes, and semaphores ".

 

Haha ^ _ ^. This series uses six articles to explain the above three tables. If you can easily write this table and explain the usage of each function, this provides a good foundation for the synchronization mutex problem of multiple threads.

 

Through studying the synchronization problem of the classic thread, We have initially developed various "tricks" to solve the mutex of multi-thread synchronization ", next we will learn two well-known examples: Second kill multi-thread Article 10 producer consumer issues and second kill multi-thread Article 11th reader writer issues to make us more proficient in multi-thread synchronization.

 

 

 

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/7538247

 

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.