[转] Thread mutual exclusion under Windwos

xiaoxiao2021-03-06  97

Thread mutual exclusion under Windwos

Keywords: windows, thread, mutual exclusive, synchronization

Abstract: Analysis of "mutual exclusive" and "synchronization" conceptual differences, briefly introduces the mutual exclusion, synchronization mechanism under the Windows platform, and discusses the producer-consumer model and its deformed, and easy error.

Overview

The network multimedia application includes simultaneous network transmission, media acquisition, and display, media data codec, human-machine interface, etc., and relatively independent components, each part needs parallel collaboration to work properly, so a large number of adopts Multi-threaded mode of operation. Multi-threaded mode has made special requirements for resources. This paper analyzes the difference in thread "mutual exclusion" and "synchronization" in concept, briefly introduces the mutual exclusion, synchronization mechanism under the Windows platform, discusses the producer-consumer model and its deformation, and easy error.

Mutually exclusive and synchronization

Mutual exclusion and synchronization are two closely related and confusing concepts.

Mutual exclusion: means a certain resource simultaneously allows an visitor to access it, with uniqueness and relief. However, mutual exclusion cannot limit visitors' visits to resources, ie accesses are disorderless.

Synchronization: refers to the orderly accessor to the resource through other mechanisms, by other mechanisms. In most cases, synchronization has implemented mutual exclusion, especially all writable resources, must be mutually exclusive. A small number of cases means that multiple visits can be allowed to access resources at the same time, such as the "first class reader model".

Mutual exclusion synchronization mechanism under Windows

Under Windows, a variety of kernel objects have implemented threads, synchronization and mutual exclusion between processes, often available:

Critical Section: The key section is not a kernel object, and the mutual exclusion of threads in the same process is implemented in the user state. Since it is not necessary to switch from the user state to the core state during use, the speed is very fast (about 20 instruction cycles on the x86 system), but the disadvantage is not to synchronize across the process, and the waiting time when blocking is not specified, only unlimited wait.

Mutex: (MUTEX): The mutex is realized with the mutex function similar to the key section, but the difference is that the mutex is a kernel object, which can achieve cross-process mutually exclusive, but need between user state and core state. Switch, the speed is much slower than the key festival (about 600 instruction cycles on the X86 system), and the waiting time at the time of blocking is specified.

Event (event): Event is also a kernel object, has two states of "signal state" and "no signal state". When a thread is waiting for an event, if the event is a signal state, it will continue, and if the event is no signal, the thread is blocked. Threads can specify waiting time at the time of blocking.

Semaphore: The signal is a resource counter. When a thread gets a signal amount, the number of semaphors is first reduced, and if the count is less than 0, then the thread is blocked; when a county is released, the signal is released The amount count first adds 1, if the count is less than or equal to 0, then awaken the blocked thread and execute it. The summary of the quantity is as follows:

1. If the counter M is greater than 0, it is indicated that there is also M resource access. At this time, the signal transmission thread is waiting for the queue to block the thread, and the new thread access resources will not be blocked;

2. If counter M et al, it means that there is no resource that can be accessed. At this point, the signal amount thread is waiting to be blocked in the queue, but the new thread access resource is blocked;

3. If the counter m is less than 0, there is no resource to access. At this time, the signal rate thread is waiting to be blocked, and the new thread access resource will be blocked; the amount of seminars are often used to ensure multiple resources Synchronize access.

Producer-consumer model

Producer-consumer model refers to:

1. Producers conduct production to put items into the warehouse, only one producer will put the item in the warehouse at the same time, if the warehouse is full, the producer is waiting.

2. Consumers take out items from the warehouse, only one consumers can take out items at the same time, if the warehouse is empty, the consumer is waiting;

3. The producer will not be taken at the same time when putting items in the warehouse;

4. Consumers will not be placed in items when consumers take objects;

In summary, it is to be mutually exclusive in the producer group or consumer group, and between the two groups is synchronized.

When there is only one producer, when consumers are not required, they only need to be synchronized between the groups. For example, you can use two Event / CriticalSECTION / MUTEX / SMAPHORE to achieve synchronization;

If there are multiple producers and consumers, then the situation will be complex, requiring an EVENT / CRITICATION / MUTEX to implement the mutual exclusion between threads, require two Semaphore to implement synchronization between two threads. An example is as follows:

Handle M_S_Producer; // Semaphore

Handle M_S_Consumer; // Semaphore

Handle M_E_Queue; // Event

/ / Assume that the warehouse holds up to M items, start the warehouse is empty

m_s_producer = createMaphore (NULL, M, M, NULL); // The initial count is M

m_s_consumer = createMaphore (NULL, 0, M, NULL); // The initial count is 0

M_E_Queue = CreateEvent (NULL, FALSE, TRUE, NULL); // Automatic type, initial state is a signal state

Producer

IF (WaitforsingleObject (m_s_producer, waittime) == Wait_Object_0)

{

IF (WaitforsingleObject (M_E_Queue, Waittime) == Wait_Object_0)

{

// Ok Now, Put Product

// RELASE COMSUMER'S SEMAPHORE

ReleaseSemaphore (M_S_CONSUMER, 1, NULL);

// set event to signal

SetEvent (m_e_queue);

Return True;

}

Else // Wait Event Time OUT

{

// Not Put Product So Release Producer? /// NOT PUT PRODUCT SO RELEASE

ReleaseSemaphore (M_S_Producer, 1, NULL);

SetEvent (m_e_queue);

Return False;

}

}

Else // Wait Semaphore Time Out

{

Return False;

}

consumer

IF (WaitforsingleObject (M_S_CONSUMER, WAITTIME) == Wait_Object_0)

{

IF (WaitforsingleObject (M_E_Queue, Waittime) == Wait_Object_0) {

// ok now, get products

// Releasee Productor's Semaphore

ReleaseSemaphore (M_S_Producer, 1, NULL);

// set event to signal

SetEvent (m_e_queue);

Return True;

}

Else // Wait Event Time OUT

{

// Not get Product So Release Consumer

ReleaseSemaphore (M_S_CONSUMER, 1, NULL);

SetEvent (m_e_queue);

Return False;

}

}

Else // Wait Semaphore Time Out

{

Return False;

}

It is particularly important to note that in this model, the waiting for mutex (this example is m_e_queue) must be placed in the second waiting position, otherwise the thread dead lock may be caused. Considering this situation: The warehouse is full and there is no thread access. At a certain moment, the producer is placed in the item, first waiting for M_E_QUEUE and passes M_E_QUEUE in a non-signal, then wait for M_S_Produce to be blocked. At this time, a consumer wants to take items, no matter what circumstances, it will be blocked when you wait M_E_QUEUE, so that we will wait for a deadlock. The position is not limited when the synchronization object is released.

Multiple producers a consumer

At this time, a commonly used deformation is used. If the warehouse is a queue (advanced first), the consumer will not be excluded.

consumer

IF (WaitforsingleObject (M_S_CONSUMER, WAITTIME) == Wait_Object_0)

{

// ok now, get products

// Releasee Productor's Semaphore

ReleaseSemaphore (M_S_Producer, 1, NULL);

Return True;

}

Else // Wait Semaphore Time Out

{

Return False;

}

At this point, consumers may have access to resources at the same time, but the synchronization mechanism between producer groups and consumer groups, and the advanced first out mechanism of the queue will guarantee even if consumers, producers have access to resources, nor A conflict occurs.

Multiple consumers are similar.

Author Blog:

http://blog.9cbs.net/coppermine/

转载请注明原文地址:https://www.9cbs.com/read-97781.html

New Post(0)