|
1 | 1 | <div align="center">
|
2 |
| - <img src="assets/logo.png" height=312> |
| 2 | + <img src="assets/logo.svg" height=312> |
3 | 3 | </div>
|
4 | 4 |
|
5 | 5 | <br/>
|
6 | 6 |
|
7 |
| -<p align="center"> |
8 |
| - A C++20 library that provides mutex protection for any object |
| 7 | +<p align="center"> |
| 8 | + A C++20 library providing mutex protection for any object |
9 | 9 | </p>
|
10 | 10 |
|
11 |
| ---- |
| 11 | +## 📦 Installation |
12 | 12 |
|
13 |
| -## ⚙️ Configuration |
14 |
| -### Tests |
15 |
| -```cmake |
16 |
| -set(lockpp_tests OFF) |
17 |
| -``` |
18 |
| -> If set to `ON`, lockpp will build tests. |
19 |
| -
|
20 |
| - |
21 |
| -## 📎 Installation |
22 |
| - |
23 |
| -> **Note** |
24 |
| -> This library requires a C++20 capable compiler. |
25 |
| -> In case you need support for C++17 checkout [version 1.0.2](https://github.com/Soundux/lockpp/releases/tag/v1.0.2) |
26 |
| -
|
27 |
| -- FetchContent |
28 |
| - |
29 |
| - ```cmake |
30 |
| - include(FetchContent) |
31 |
| - FetchContent_Declare(lockpp GIT_REPOSITORY "https://github.com/Soundux/lockpp") |
| 13 | +* Using [CPM](https://github.com/cpm-cmake/CPM.cmake) |
| 14 | + ```cmake |
| 15 | + CPMFindPackage( |
| 16 | + NAME lockpp |
| 17 | + VERSION 2.3 |
| 18 | + GIT_REPOSITORY "https://github.com/Soundux/lockpp" |
| 19 | + ) |
| 20 | + ``` |
32 | 21 |
|
33 |
| - FetchContent_MakeAvailable(lockpp) |
34 |
| - target_link_libraries(<YourLibrary> lockpp) |
35 |
| - ``` |
| 22 | +* Using FetchContent |
| 23 | + ```cmake |
| 24 | + include(FetchContent) |
36 | 25 |
|
37 |
| -- Git Submodule |
| 26 | + FetchContent_Declare(lockpp GIT_REPOSITORY "https://github.com/Soundux/lockpp" GIT_TAG v2.3) |
| 27 | + FetchContent_MakeAvailable(lockpp) |
38 | 28 |
|
39 |
| - ```bash |
40 |
| - git submodule add "https://github.com/Soundux/lockpp" |
41 |
| - ``` |
42 |
| - ```cmake |
43 |
| - # Somewhere in your CMakeLists.txt |
44 |
| - add_subdirectory("<path_to_lockpp>") |
45 |
| - target_link_libraries(<YourLibrary> lockpp) |
46 |
| - ``` |
| 29 | + target_link_libraries(<target> lockpp) |
| 30 | + ``` |
47 | 31 |
|
48 |
| -## 📔 Usage |
| 32 | +## 📃 Usage |
49 | 33 |
|
50 |
| -### Simple example |
51 | 34 | ```cpp
|
52 |
| -#include <lockpp/lock.hpp> |
| 35 | +lockpp::lock<std::string> var("Test"); |
53 | 36 |
|
54 |
| -int main() |
| 37 | +// Read only access |
55 | 38 | {
|
56 |
| - lockpp::lock<std::string> protected_string("Test"); |
57 |
| - { |
58 |
| - auto read_access = protected_string.read(); |
59 |
| - bool is_empty = read_access->empty(); |
60 |
| - // read_access->clear(); <-- Will not work |
61 |
| - } |
62 |
| - { |
63 |
| - auto write_access = protected_string.write(); |
64 |
| -
|
65 |
| - write_access->clear(); |
66 |
| - *write_access = "New String!"; |
67 |
| -
|
68 |
| - // auto another_write_access = protected_string.write(); <-- This will lock. |
69 |
| - } |
70 |
| -
|
71 |
| - return 0; |
| 39 | + auto locked = var.read(); |
| 40 | + assert(!locked->empty()); |
72 | 41 | }
|
73 |
| -``` |
74 | 42 |
|
75 |
| -### Custom Mutex |
76 |
| -```cpp |
77 |
| -#include <lockpp/lock.hpp> |
78 |
| - |
79 |
| -int main() |
| 43 | +// Write access |
80 | 44 | {
|
81 |
| - lockpp::lock<std::string, std::recursive_mutex> protected_string("Test"); |
82 |
| - { |
83 |
| - auto write_access = protected_string.write(); |
84 |
| - |
85 |
| - write_access->clear(); |
86 |
| - *write_access = "New String!"; |
87 |
| - |
88 |
| - auto another_write_access = protected_string.write(); |
89 |
| - *another_write_access = "This is fine!"; |
90 |
| - } |
| 45 | + auto locked = var.write(); |
91 | 46 |
|
92 |
| - return 0; |
| 47 | + *write_access = "assignment"; |
| 48 | + locked->clear(); |
93 | 49 | }
|
94 |
| -``` |
95 |
| - |
96 |
| -### Custom Locks |
97 |
| -```cpp |
98 |
| -#include <lockpp/lock.hpp> |
99 |
| - |
100 |
| -int main() |
101 |
| -{ |
102 |
| - lockpp::lock<std::string> protected_string("Test"); |
103 |
| - { |
104 |
| - auto read_1 = test.read<std::unique_lock>(); |
105 |
| - auto read_2 = test.read<std::unique_lock>(std::adopt_lock); // <-- Also accepts custom arguments for lock |
106 |
| - } |
107 | 50 |
|
108 |
| - return 0; |
109 |
| -} |
| 51 | +// One time access |
| 52 | +var.assign("another assignment"); |
| 53 | +assert(var.copy() == "another assignment"); |
110 | 54 | ```
|
111 | 55 |
|
112 |
| -> For more examples see [tests](tests/) |
| 56 | +_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`)_. |
| 57 | +
|
| 58 | +> For more examples see [tests](tests) |
0 commit comments