Turn----seconds to kill multithreading Nineth Classic thread synchronization summary key segment event Mutex semaphore

Source: Internet
Author: User

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 mutexmutex

" 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 waiting for the thread b after the operation, 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

Destroyed

Enter the mutex area

Leave the mutex area

Critical section CS

initialize-

CriticalSection

delete-

CriticalSection

enter-

CriticalSection

leave-

criticalsection

mutex

createmutex

closehandle

Wait 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 has 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

Turn----seconds to kill multithreading Nineth 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.