Classic Thread synchronization Summary key segment event Mutex semaphore

Source: Internet
Author: User
Tags mutex semaphore

In front of the second multi-threaded fourth a classic multi-threaded synchronization problem, proposed a classic multi-threaded synchronization mutex problem, This problem includes the main thread and Sub-thread synchronization, Sub-thread Mutual exclusion, is a very classic multi-threaded synchronization mutex problem example, followed by four

"second-kill multithreaded fifth" Classic thread synchronization key section CS

"second-kill multithreaded sixth" Classic thread synchronization events event

"second-kill multithreaded seventh" Classic thread synchronization mutex mutex

"second-kill multithreaded eighth" Classic thread synchronization signal volume semaphore

To detail the common thread synchronization mutex Mechanism--key segment, event, Mutex amount, semaphore. Here is a summary of them to help you comb the various points of Knowledge.

First of all, on the concept of thread synchronization mutual exclusion of knowledge, I believe you through the previous article, has a certain understanding of thread synchronization mutex, can also be vague to say the thread synchronization mutually exclusive knowledge of the various concepts, the following list from the "computer operating system" in the book selected some of the thread synchronization mutex Description. Believe that there is a preliminary and vague impression and then look at the definition of authority, it should be remembered particularly deep.

1. Main tasks for thread (process) synchronization

A: After the introduction of multi-threading, due to the asynchronous execution of the thread, it will cause confusion to the system, especially in emergency critical resources, such as multiple threads emergency the same printer, will make printing results intertwined, difficult to Distinguish. When multiple threads are in emergency of sharing variables, tables, and linked lists, There may be errors in data processing, so the main task of thread synchronization is to enable the concurrent execution of threads to effectively share resources and cooperate with each other, thus making the execution of the program Reproducible.

2. What are the constraints between threads (processes)?

When a thread executes concurrently, the following two constraints exist between use threads due to resource sharing and thread Collaboration.

(1). Indirect mutual Restraint. Multiple threads in a system must share some system resources, such as shared cpus, shared I/O devices, and so-called indirect mutual constraints originate from this sharing of resources, and printers are the best examples, while thread a waits for other threads to use the Printer.

(2). Direct Mutual Restriction. This restriction is mainly due to the co-operation between threads, such as thread a, which provides the result of the calculation to thread B for further processing, and Threads B will be in a blocked state before the data is Delivered.

Indirect mutual restriction can be called mutual exclusion , direct mutual restriction can be called synchronization , for mutual exclusion can be understood, thread A and thread B mutually exclusive access to a resource, they will produce a sequential problem--or thread A waits for thread B to finish, Either thread B waits for the thread to finish, which is actually the thread Synchronization. So synchronization includes mutual exclusion, and mutual exclusion is actually a special kind of synchronization .

3. Critical resources and critical areas

A resource that allows only one thread to access for a period of time is called a critical or exclusive resource, and most physical devices in the computer, the shared variable waits in the process are critical resources, and they require mutually exclusive access. The code that accesses critical resources in each process is called a critical section

After reading the concept of knowledge, the following table to help you better memory and use of multi-threaded synchronous mutual exclusion of four implementation Methods-key segments, events, mutexes, semaphores.

Critical segment CS and mutex mutex

 ,

Create or initialize

criticalsection

enter-

CriticalSection

leave-

criticalsection

createmutex

closehandle

Wait for series functions such as WaitForSingleObject

releasemutex

The key segment and the mutex have "thread ownership" concept, you can understand "thread ownership" into the Hotel's room card, in the hotel front desk registered name has a room card can be multiple access to the room, others will not be able to enter until you hand over the room Card. Each thread must first attempt to obtain "thread ownership" through EnterCriticalSection or WaitForSingleObject to invoke LeaveCriticalSection or Releasemutex. Otherwise the call fails, which is equivalent to the forgery of the room card to check-out-the registration will be rejected because it does not have your Name.

The mutex can handle the "abandonment" situation very well, so it can be used safely between the multiple Processes.

Events Event

Create

Destroyed

Make an Event Trigger

Cause the event not to fire

Events Event

CreateEvent

CloseHandle

SetEvent

ResetEvent

Note that the manual and automatic placement of events should be clearly divided and not confused.

Signal Volume semaphore

Create

Destroyed

Decrement count

Increment count

Signal Volume

Semaphore

create-

Semaphore

CloseHandle

Wait for series functions such as WaitForSingleObject

release-

Semaphore

The semaphore indicates a trigger state when the count is greater than 0, the call WaitForSingleObject does not block, equals 0 means the state is not triggered, and the call WaitForSingleObject blocks until another thread increments the Count.

Note: mutexes, events, semaphores are kernel objects that can be used across processes (via openmutex,openevent,opensemaphore). however, Why only the mutex can solve the "abandonment" situation, please see the second multi-threaded 15th key section, events, Mutual exclusion, signal volume of "abandonment" problem.

Hehe ^_^, This series used six articles to explain the above three tables, if the reader can easily write this table and can explain the use of the function, then the multi-threaded synchronization mutex problem also has a good foundation.

Through the classic thread synchronization problem learning, We have been preliminary training to solve the multi-thread synchronization mutual exclusion of various "moves", the following through the study of two well-known examples of the second multi-threaded tenth producer consumer problems and "seconds kill multi-threaded 11th reader writer problem" to enable us in the solution of multi-threaded synchronization more Skilled.

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

Classic Thread synchronization Summary key segment event Mutex semaphore

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.