synchronization primitives - sergeyzolotukhin/learn-java-gradle GitHub Wiki

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

โš ๏ธ **GitHub.com Fallback** โš ๏ธ