Recently reviewed the next multi-threaded concepts, the main is the synchronization between the threads and mutual exclusion, simple record.
Basic Concepts
2 Basic Concepts:
Sync: Run in a predetermined sequence. For example: a.b.c3 a thread, a runs first, then B, then C. Mutex: A resource that allows only one visitor to access it at the same time, is unique and exclusive. However, mutual exclusion cannot limit the order in which visitors access resources, that is, access is unordered.related Functions Critical Zone/critical area critical_sectionThe use of critical sections is simple, the basic steps are: Initialize--enter--leave-Destroy initialization: InitializeCriticalSection: EnterCriticalSection Left: leavecriticalsection Destroyed: DeleteCriticalSection
The most efficient, single-process mutual exclusion is the best.
The concept of thread ownership, so the current can enter the critical section of the threads can enter the critical section more than once, so do not synchronize.
Mutex MutexUse of Mutex basic steps: Create-Wait Object-release--Close create: CreateMutex Wait object: WaitForSingleObject release: ReleaseMutex off: CloseHandle
Cross-process, efficiency in general, can be used to ensure that only one instance is executed, mainly mutex.
The mutex threads ownership, so the wait-to thread can wait multiple times and then enter the relevant area.
Events EventBasic usage steps for an event: Create--set--wait for--reset--to close creation: CreateEvent set:setevent wait: WaitForSingleObject reset:resetevent or Auto reset off after WaitForSingleObject: CloseHandle
Cross-process, set can wait until after reset, wait. There is a pulseevent function, pretty interesting, that the function is set immediately after reset, equivalent to firing a pulse.
It is stuck there without triggering, so it can be used as a synchronization. If you set an automatic reset, then after wait, reset, then you will definitely not be able to enter again.
Signal Volume SemaphoreSemaphores are similar to events, basic use steps: Create-release resources-wait-release resources-Close create: CreateSemaphore release resources: ReleaseSemaphore wait: WaitForSingleObject Close: CloseHandle
After the process waits for the function to wait successfully, the resource count will be-1. If the current resource count is sufficient, you can still wait. Note: Release amount cannot exceed the total number of resources set.
Similar events, no resources will be stuck there.
Comparative AnalysisAll available for mutually exclusive event and semaphore can be used for synchronous criticalsection non-kernel objects, relatively more efficient
The other 3 are kernel objects that can be cross-process and can be used to ensure that only one instance runs another 3 have corresponding Open function event can be automatically reset, convenient semaphore can specify the number of resources the mutex can handle the "abandonment" problem very well, that is, the process end is not released, it is automatically released, For multi-process simultaneous use of a name of the mutex, this is a good news above, everyone has advantages and disadvantages, the use of the need to choose according to the actual situation.