InitializeCriticalSection

Initializes a critical section object.

Syntax

VOID InitializeCriticalSection(
  LPCRITICAL_SECTION lpCriticalSection
);

Parameters

  • lpCriticalSection

    A pointer to the critical section object to be initialized. This structure must be allocated by the calling process.

Remarks

A critical section object provides a simple mechanism for mutual-exclusion synchronization that can be used by threads of the same process to prevent race conditions when accessing shared resources. A thread uses the EnterCriticalSection function to request ownership of the critical section. If the object is already owned by another thread, the calling thread enters a waiting state until the object is released.

The InitializeCriticalSection function initializes the critical section object. You must initialize a critical section object before using it.

For critical section objects that are dynamically allocated, use the DeleteCriticalSection function to deallocate them.

Threads within the same process can use critical section objects to protect shared data. Critical sections are not suitable for synchronizing threads in different processes because they are not shareable across process boundaries.

If initialization fails, the system raises a STATUS_NO_MEMORY exception.

Return Value

This function does not return a value.

Requirements

#include <windows.h>

Example

The following example demonstrates how to initialize and use a critical section object.

#include <windows.h>
#include <iostream>

// A critical section object
CRITICAL_SECTION g_criticalSection;

// A shared resource
int g_sharedCounter = 0;

// A thread function that increments the counter
DWORD WINAPI IncrementCounterThread(LPVOID lpParam) {
    // Wait until the critical section is available, then acquire it
    EnterCriticalSection(&g_criticalSection);

    // Access the shared resource
    g_sharedCounter++;
    std::cout << "Counter: " << g_sharedCounter << std::endl;

    // Release the critical section
    LeaveCriticalSection(&g_criticalSection);

    return 0;
}

int main() {
    // Initialize the critical section object
    InitializeCriticalSection(&g_criticalSection);

    HANDLE hThread1, hThread2;
    DWORD dwThreadId1, dwThreadId2;

    // Create two threads that will increment the shared counter
    hThread1 = CreateThread(
        NULL,       // Default security attributes
        0,          // Default stack size
        IncrementCounterThread, // Thread function
        NULL,       // Parameter to thread function
        0,          // Default creation flags
        &dwThreadId1); // Receives the thread identifier

    hThread2 = CreateThread(
        NULL,       // Default security attributes
        0,          // Default stack size
        IncrementCounterThread, // Thread function
        NULL,       // Parameter to thread function
        0,          // Default creation flags
        &dwThreadId2); // Receives the thread identifier

    // Wait for the threads to complete
    if (hThread1 != NULL) {
        WaitForSingleObject(hThread1, INFINITE);
        CloseHandle(hThread1);
    }
    if (hThread2 != NULL) {
        WaitForSingleObject(hThread2, INFINITE);
        CloseHandle(hThread2);
    }

    // Delete the critical section object
    DeleteCriticalSection(&g_criticalSection);

    std::cout << "Final Counter Value: " << g_sharedCounter << std::endl;

    return 0;
}