Modern C++ mutex/shared_mutex guard for T, similar to Rust.
Simple exclusive-lock mutex.
Read/Write (Shared/Exclusive) mutex.
int main() {
guards::shared_mutex guarded_data(std::vector<int>{});
std::thread([&] {
while (true) {
try {
auto r = guarded_data.read();
if (!r->empty())
std::cout << r->back() << ' ' << r->size() << std::endl;
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
break;
}
usleep(500);
}
}).detach();
std::thread([&] {
while (true) {
try {
auto w = guarded_data.write();
if (w->empty())
w->push_back(1);
else w->push_back(w->back() + 1);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
break;
}
usleep(1000);
}
}).detach();
// spin as threads are running
while (true) {
usleep(100000);
}
}
- macOS 13.3.1 22E261 x86_64