Windows Kernel Synchronization API

This section details the core synchronization primitives available in the Windows Kernel for managing concurrent access to shared resources and coordinating thread execution.

Mutexes

Mutexes (Mutual Exclusion objects) are used to protect shared data from being accessed by multiple threads simultaneously. Only one thread can own a mutex at a time.

CreateMutex

Creates or opens a mutex object.

HANDLE CreateMutex(
  LPSECURITY_ATTRIBUTES lpMutexAttributes,
  BOOL                bInitialOwner,
  LPCTSTR             lpName
);
Parameters:
  • lpMutexAttributes: Pointer to a SECURITY_ATTRIBUTES structure.
  • bInitialOwner: If TRUE, the calling thread becomes the initial owner.
  • lpName: Optional name for the mutex.
Return Value:
  • A handle to the mutex object, or NULL on failure.
Remarks:
  • Use CloseHandle to close the mutex handle when no longer needed.

ReleaseMutex

Releases ownership of a mutex object.

BOOL ReleaseMutex(
  HANDLE hMutex
);
Parameters:
  • hMutex: A handle to the mutex object.
Return Value:
  • TRUE if the mutex was successfully released, FALSE otherwise.

Events

Event objects are used to signal the occurrence of an event between threads. One thread can signal an event, and other threads can wait for it to be signaled.

CreateEvent

Creates or opens an event object.

HANDLE CreateEvent(
  LPSECURITY_ATTRIBUTES lpEventAttributes,
  BOOL                bManualReset,
  BOOL                bInitialState,
  LPCTSTR             lpName
);
Parameters:
  • lpEventAttributes: Pointer to SECURITY_ATTRIBUTES.
  • bManualReset: If TRUE, manual reset; if FALSE, auto-reset.
  • bInitialState: TRUE for signaled, FALSE for non-signaled.
  • lpName: Optional name for the event.
Return Value:
  • A handle to the event object, or NULL on failure.

SetEvent

Sets the state of the specified event object to signaled.

BOOL SetEvent(
  HANDLE hEvent
);

ResetEvent

Sets the state of the specified event object to non-signaled.

BOOL ResetEvent(
  HANDLE hEvent
);

WaitForSingleObject

Waits until the specified object is in the signaled state or the time-out interval elapses.

DWORD WaitForSingleObject(
  HANDLE hHandle,
  DWORD  dwMilliseconds
);
Parameters:
  • hHandle: Handle to the object.
  • dwMilliseconds: Time-out interval in milliseconds.
Return Value:
  • WAIT_OBJECT_0 if the object is signaled.
  • WAIT_TIMEOUT if the time-out interval elapses.

Semaphores

Semaphores are synchronization objects that act as counters. They are useful for limiting the number of threads that can access a particular resource or resource pool.

CreateSemaphore

Creates or opens a semaphore object.

HANDLE CreateSemaphore(
  LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,
  LONG                  lInitialCount,
  LONG                  lMaximumCount,
  LPCTSTR               lpName
);
Parameters:
  • lpSemaphoreAttributes: Pointer to SECURITY_ATTRIBUTES.
  • lInitialCount: Initial count of the semaphore.
  • lMaximumCount: Maximum count of the semaphore.
  • lpName: Optional name for the semaphore.
Return Value:
  • A handle to the semaphore object, or NULL on failure.

ReleaseSemaphore

Increments the count of a semaphore object, releasing one or more threads that are waiting for the semaphore.

BOOL ReleaseSemaphore(
  HANDLE hSemaphore,
  LONG   lReleaseCount,
  LPLONG lpPreviousCount
);

Critical Sections

Critical sections provide a lightweight mechanism for mutual exclusion. They are typically used for protecting shared data within a single process.

InitializeCriticalSection

Initializes a critical section object.

VOID InitializeCriticalSection(
  LPCRITICAL_SECTION lpCriticalSection
);

EnterCriticalSection

Enters a critical section. If the critical section is already entered by another thread, the calling thread is suspended until the critical section is entered.

VOID EnterCriticalSection(
  LPCRITICAL_SECTION lpCriticalSection
);

LeaveCriticalSection

Leaves a critical section object that was previously entered by the calling thread.

VOID LeaveCriticalSection(
  LPCRITICAL_SECTION lpCriticalSection
);

DeleteCriticalSection

Deinitializes a critical section object.

VOID DeleteCriticalSection(
  LPCRITICAL_SECTION lpCriticalSection
);

Interlocked Operations

Interlocked operations provide a set of atomic routines that perform simple arithmetic or manipulate shared variables without causing race conditions.

InterlockedIncrement

Atomically increments the value of a given variable.

LONG InterlockedIncrement(
  LPLONG Addend
);

InterlockedDecrement

Atomically decrements the value of a given variable.

LONG InterlockedDecrement(
  LPLONG Addend
);

InterlockedExchange

Atomically exchanges the values of two variables.

LONG InterlockedExchange(
  LPLONG Target,
  LONG   Value
);

InterlockedCompareExchange

Atomically compares two integers and, if they are equal, stores the third integer into the address of the second integer and returns the original value of the second integer.

LONG InterlockedCompareExchange(
  LPLONG Destination,
  LONG   Exchange,
  LONG   Comparand
);