Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/curve/lockpp

🔒 A C++20 Library that provides mutex protected objects
https://github.com/curve/lockpp

cmake cpp-library cpp20 cpp20-library cpp23 cpp23-library mutex mutex-lock mutex-locks safety thread-safe thread-safety

Last synced: 2 months ago
JSON representation

🔒 A C++20 Library that provides mutex protected objects

Awesome Lists containing this project

README

        





A C++20 library providing mutex protection for any object

## 📦 Installation

* Using [CPM](https://github.com/cpm-cmake/CPM.cmake)
```cmake
CPMFindPackage(
NAME lockpp
VERSION 3.0
GIT_REPOSITORY "https://github.com/Curve/lockpp"
)
```

* Using FetchContent
```cmake
include(FetchContent)

FetchContent_Declare(lockpp GIT_REPOSITORY "https://github.com/Curve/lockpp" GIT_TAG v3.0)
FetchContent_MakeAvailable(lockpp)

target_link_libraries( cr::lockpp)
```

## 📃 Usage

```cpp
lockpp::lock 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](tests/custom-mutex.cpp) as well [as custom locks](tests/custom-lock.cpp) _(i.e `std::unique_lock`, `std::lock_guard`)_.

> For more examples see [tests](tests)