Skip to content

synchronization primitives

serhij.zolotukhin edited this page Jun 29, 2026 · 1 revision

synchronization primitives

mutexes

A mutex (short for mutual exclusion) in C++ is a synchronization primitive used to protect shared data from being accessed by multiple threads simultaneously. It acts as a lock, ensuring that only one thread can execute a specific section of code, known as a critical section, at any given time.

semaphore

A semaphore contains an integer value, initialized in its constructor, which represents 𝐭𝐡𝐞 𝐦𝐚𝐱𝐢𝐦𝐮𝐦 𝐧𝐮𝐦𝐛𝐞𝐫 𝐨𝐟 𝐜𝐨𝐧𝐜𝐮𝐫𝐫𝐞𝐧𝐭 𝐭𝐡𝐫𝐞𝐚𝐝𝐬 that are allowed to access a shared resource, such as 𝘴𝘩𝘢𝘳𝘦𝘥 𝘮𝘶𝘵𝘢𝘣𝘭𝘦 𝘥𝘢𝘵𝘢.

condition variable

A condition variable enables a thread to sleep until another thread signals that a condition has changed. It’s used alongside a mutex to protect shared state and coordinate access.

https://medium.com/@kamresh485/semaphores-in-c-b0e98ee95436

lock

A lock guard is an RAII (Resource Acquisition Is Initialization) wrapper around a mutex that automatically acquires a lock when created and releases it when destroyed. This ensures that the mutex is properly unlocked even if an exception is thrown.

C++ also provides various lock types that handle the locking and unlocking of mutexes automatically. These are more sophisticated than a simple mutex and use RAII (Resource Acquisition Is Initialization) principles to manage mutexes. Commonly used locks include std::lock_guard and std::unique_lock.

https://www.simplifycpp.org/?id=a0378 https://medium.com/@AlexanderObregon/concurrency-and-multithreading-in-c-5ede6aa06241

spinlock

A spinlock is another type of synchronization primitive that is designed to be lightweight and efficient when a lock is held for a short duration. A thread attempting to acquire a spinlock will “spin” in a loop, repeatedly checking if the lock is available. This technique can be efficient when the expected wait time is minimal.

https://namastedev.com/blog/understanding-the-role-of-spinlocks-and-mutexes-in-thread-synchronization/

latch

A std::latch is a synchronization primitive that permits a certain number of count_down operations (decrements) before allowing one or more threads to pass the wait point.

https://www.kdab.com/synchronization-primitives-in-c20/

barrier

It's designed to make multiple threads wait until they all reach the barrier point and then proceed together

https://www.kdab.com/synchronization-primitives-in-c20/ https://medium.com/@ibrahim__/understanding-synchronization-primitives-and-techniques-in-multithreaded-programming-565b71017d0e

Monitor Object - concurrency pattern

It introduces a high-level synchronization primitive called a monitor

Before the Monitor Object pattern, managing concurrency often involved using raw synchronization primitives like mutexes and condition variables scattered throughout a codebase

https://medium.com/@sachinklocham/the-restroom-of-concurrency-understanding-monitor-objects-in-c-775a73786db8 https://www.ersoftware.nl/synchronization-primitives/ https://www.modernescpp.com/index.php/monitor-object/

theory

critical section

A critical section is a segment of code in a multithreaded program where shared resources (such as variables, files, or data structures) are accessed. This section must be protected to ensure that only one thread can execute it at a time.

open questions

  • promise, future, async
  • atomic - A Lock-Free Alternative

https://en.wikipedia.org/wiki/Synchronization_(computer_science)

best practices

https://medium.com/@AlexanderObregon/concurrency-and-multithreading-in-c-5ede6aa06241

My side bar

Clone this wiki locally