A C++20 library providing mutex protection for any object
-
Using CPM
CPMFindPackage( NAME lockpp VERSION 3.0 GIT_REPOSITORY "https://github.com/Curve/lockpp" )
-
Using FetchContent
include(FetchContent) FetchContent_Declare(lockpp GIT_REPOSITORY "https://github.com/Curve/lockpp" GIT_TAG v3.0) FetchContent_MakeAvailable(lockpp) target_link_libraries(<target> cr::lockpp)
lockpp::lock<std::string> var("Test");
// Read only access
{
auto locked = var.read();
assert(!locked->empty());
}
// Write access
{
auto locked = var.write();
*write_access = "assignment";
locked->clear();
}
// One time access
var.assign("another assignment");
assert(var.copy() == "another assignment");
lockpp also allows you to supply the mutex to be used as well as custom locks (i.e std::unique_lock
, std::lock_guard
).
For more examples see tests