Seconds to kill multithreading Nineth Classic thread synchronization summary key segment event Mutex semaphore

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.