Synchronization Primitives

Synchronization Primitives

A quick guide to mutexes, shared locks, semaphores, latches, barriers, and coordination patterns.

Synchronization Primitives

Core building blocks

Reader-writer locking

std::shared_mutex mutex;

void read_value() {
    std::shared_lock lock(mutex);
}

void write_value() {
    std::unique_lock lock(mutex);
}

Semaphores

std::counting_semaphore<4> slots(4);

Use semaphores when access should be capped instead of fully serialized.

Coordination tools

Practical guidance