Support for multi-threaded synchronization and mutex operations in Windows environments consists of four main ways: critical section (CriticalSection), mutex (mutex), Semaphore (Semaphore), event object. The following are descriptions of these four ways:
(1) critical section (criticalsection)
The code that accesses the critical resource in each process is called a critical section (a critical resource is a shared resource that only one process is allowed to use at a time). Only one process is allowed to enter the critical section at a time, and no other process is allowed to enter. Whether it is a hardware critical resource or a software critical resource, multiple processes must be mutually exclusive to access it. There are several basic operations for critical sections in the Windows environment:
critical_section criticalsection;initializecriticalsection (&criticalsection); EnterCriticalSection (&criticalsection); LeaveCriticalSection (&criticalsection);D eletecriticalsection (&criticalsection);
(2) Mutex object (mutex)
In programming, the concept of an object mutex (also called a mutex) is introduced to guarantee the integrity of the shared data operation. Each object corresponds to a tag that can be called a "mutex", which is used to guarantee that only one thread can access the object at any one time. There are several operating interfaces for mutex objects:
Createmutexopenmutexreleasemutex
Use WaitForSingleObject when using mutex objects, for example:
WaitForSingleObject (/*...*/); Do_something (); ReleaseMutex (/*...*/);
(3) Signal Volume (Semaphore)
semaphores, sometimes called semaphores, are a facility used in multi-threaded environments that coordinate individual threads to ensure that they are able to use public resources correctly and rationally. It is also the amount of the operating system that controls process synchronization mutex . The semaphore is divided into single-valued and multi-valued two, the former can only be obtained by one thread, the latter of which may be obtained by several threads. Compared with the mutex object, the semaphore is like a house that can accommodate more than n people to allow more than one person to enter at the same time (the number of limited), and the mutex can only accommodate a person's small house, at the same time only one person to use.
The semaphore operation interfaces in the Windows environment include:
Createsemaphoreopensemaphorereleasesemaphore
The semaphore is used in the same way as a mutex, except that the number of signals needs to be specified at initialization time:
WaitForSingleObject (/*...*/); Do_something (); ReleaseSemaphore (/*...*/);
(4) Events Object (event)
The event object is a very interesting lock result under windows. In a sense, it is similar to a mutex, but not the same. Because a thread is often required to invoke a setevent setting before it can be used for the lock, it is usually necessary to call one of the threads (possibly the main thread or other threads). The point is, before the thread ends, we don't know where the current threads are executing after the event. So use it and be very careful. Common event object operations are:
Createeventopeneventpulseeventreseteventsetevent
The main thread can generally do this:
CreateEvent (/*...*/); Create Event Object SetEvent (/*...*/); Set signal waitformultiobjects (Hthread,/*...*/); Wait for thread to end CloseHandle (/*...*/); To close a thread handle
The thread that is started is typically waiting for an event to move:
while (1) { WaitForSingleObject (/*...*/); Wait for event /*...*/}
Summarize:
(1) There are sample code on MSDN for critical section, mutex area, semaphore, event.
(2) In general, use frequency on semaphore > Mutex object > Critical section > Event Object
(3) The signal volume can realize the function of other three kinds of locks, the learning should be focused on
(4) The end of the paper on the light, more practice to master the difference between them
Windows synchronization with multithreaded programming