C++ Mutex Optimization

C++ Mutex Optimization

Basics of Locks Locks are the core synchronization primitives in C++ concurrent programming, used to protect access to shared resources, ranging from simple variables to complex code segments. The C++11 standard library provides two core types of locks: mutex: an exclusive mutex, allowing only one thread to hold it at a time. shared_mutex: a shared-exclusive … Read more

How Does C++ Read-Write Lock (shared_mutex) Work?

How Does C++ Read-Write Lock (shared_mutex) Work?

Sometimes, we encounter a situation where: “many reads, very few writes.” For example, configuration, status information, coordinate parameters, etc. If all threads use <span>std::mutex</span>, then read operations will queue up, wasting performance, which is unfortunate 😅 To solve this problem, C++17 introduced a smarter lock 🔒: “std::shared_mutex”. Why is it called a Read-Write Lock? You … Read more