Use the Kernel Object synchronization thread

Source: Internet
Author: User

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

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.