The kernel object can be used for thread synchronization between different processes.
1. WaitSeries Functions
Waitforsingleobject
Suspends the current thread until the function returns.(The waiting object is inSignalStatus or wait timeout)
if multiple threads call this function to wait for the same kernel object, when this kernel object is in signal only one thread is awakened.
If the thread has been createdWindowsOfCode,RequiredMsgwaitformultipleobjectsReplaceWaitforsingleobject,Otherwise, a deadlock occurs.
Waitformultipleobjects
If you need to wait for Multiple kernel objects at the same time,You can call this function.
2.Event Kernel Object
Automatic event
When the event is inSignalStatus,Only oneThe thread waiting for the event will be awakened.The event is automatically returned.Non SignalStatus.
Manual event
When the event is inSignalStatus,AllAll threads waiting for this event will be awakened.Call laterReseteventSetEventObject callbackNon SignalStatus.
3.Semaphores
Semaphores can control the number of threads that can run at a time.
Use an initial value to initialize a semaphore object.Lmaximumcount (Maximum number of threads allowed to run), AndLinitialcountAssignedCurrentsource
Internal use of semaphoresCurrentsourceTo record the number of currently running threads,.
WhenCurrentsourceGreater0The semaphore is inSignalStatus. WhenCurrentsource = 0Hour
WhenCurrentsource = 0The semaphore is inNon SignalStatus.
WaitforsingleobjectMakeCurrentsourceMinus 1, ReleasesemaphoreMakeCurrentsource + 1.
4.Mutex lock
Wait_abandoned
InWindowsUsing mutex can effectively avoid deadlocks.
When holdingMutexThe thread is callingReleasemutexQuit unexpectedly, as shown in figureExitthreadAndTerminatethread. The system willMutexDistribute to waitMutexAnd returnsWait_abandoned,This avoids deadlocks.
5.Other Synchronization Methods
Asynchronous device I/O
Waitforinputidle
Msgwaitformultipleobjects (Ex)
Waitfordebugevent
Signalobjectandwait
Detecting deadlocks with the wait chain traversal API
6. Common API:
Waitforsingleobject waitformultipleobjects
Msgwaitformultipleobjects
Createevent createeventex setevent resetevent openevent
Createsemaphore opensemaphore
Createmutex openmutex releasemutex