Synchronization

Multiple threads can read shared data simultaneously as long as no one is writing. However, whenever any of those threads write, it introduces a class of concurrency bugs called data races. A data race occurs when:

There is also a broader concurrency class of concurrency bugs called race conditions. This type of bug arises when the program's correctness depends on the timing or ordering of events (like thread scheduling), and different orderings produce different results. To prevent data races and race conditions, we must introduce synchronization primitives.

Mutex Lock

...

Readers-writer Lock

A readers-writer lock (std::sync::RwLock) is a synchronization primitive for multi-threaded shared mutable state that allows multiple readers OR one writer at a time. It is best used for scenarios where reads are frequent and writes are infrequent. It has two key methods:

Condition Variables

...

Semaphore

...