C++ optimizing multi-threaded application
Mar 4, 2021
Modern C++ has all the tools for thread optimization
std::shared_lock
- The class
shared_lock
is a general-purpose shared mutex ownership wrapper allowing deferred locking, timed locking, and transfer of lock ownership. - Locking a
shared_lock
locks the associated shared mutex in shared mode (to lock it in exclusive mode, std::unique_lock can be used) - The
shared_lock
class is movable, but not copyable -- it meets the requirements of MoveConstructible and MoveAssignable but not of CopyConstructible or CopyAssignable. - In a
reader
andwrite
the situation (or producers-consumers), where there can be multiple readers and writers, usestd::shared_lock for readers, and use std::unique_lock for writers because only writers can change the state so they need the exclusiveness.