synchronized
is a utility class meant to almost completely replace manual usage of mutexes and condition variables in C++,
in the same way that smart pointers replace owning raw pointers.
The project originally grew out of a lightning talk at CppCon 2023 about
a version of synchronized_value
, which has been proposed for the C++ standard. Based on the feedback and inspiration from similar
projects, I have decided to redesign the abstraction.
A simple but complete example can be found in examples/synchronized_stack.h
:
class synchronized_stack {
sv::synchronized<std::stack<int>> data_;
public:
using value_type = Element;
void push(int d) { data_.wlock()->push(d); }
std::optional<int> pop() {
auto lock = data_.wlock();
if (lock->empty()) return {};
auto result = lock->top();
lock->pop();
return result;
}
auto empty() { return data_.rlock()->empty(); }
auto size() { return data_.rlock()->size(); }
};
- Make it easy to fix broken concurrent code / safely add concurrency to sequential code.
- Be a zero cost abstraction.
- Support enough features that users have no need to use mutexes directly.
-
Const should propagate, so
synchronized<T> const
is equivalent tosynchronized<T const>
. -
Timed mutex types.
-
Sharing a mutex between several update guards, analogously to how several
std::shared_ptr
s can share a count. Use case: Locking head/tail in theconcurrent_doubly_linked_list
example. -
Make update_guard movable/swappable, to facility hand-over-hand locking.
-
Support for alternative mutex types, e.g.
std::shared_mutex
,std::timed_mutex
, etc. -
Support for conditional critical sections
-
Hierarchies of synchronized values, for working with arena allocators:
To read, only lock the value in read mode. To write, lock both the value and the arena in write mode.
CsLibGuarded: "... a standalone header only library for multithreaded programming."
A very mature library, which is part of CopperSpice.
Synchronized<T>
from folly: Very well-designed abstraction. Inspired synchronized
's interface.
data_mutex by Peter Most:
Very similar to the synchronized_value
proposed for the C++ standard, but with different names for the classes.
Includes support for const access. Part of CppAidKit
Boost's synchronized_value: Essentially the same interface as the one proposed for the standard, with a few features added.
The current standard proposal is described in documents number N4033 and p0290r4.