From 763f0ed522044adec12eaa1a768edfb1a98a635a Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 6 Jun 2026 17:49:56 +0200 Subject: foo.cpp -> test/threads.cpp --- test/threads.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/threads.cpp (limited to 'test') diff --git a/test/threads.cpp b/test/threads.cpp new file mode 100644 index 0000000..a7891bb --- /dev/null +++ b/test/threads.cpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace { + +struct Counter { + std::mutex mtx; + std::size_t impl = 0; +}; + +void do_something(Counter& counter) { + std::lock_guard lck{counter.mtx}; + ++counter.impl; + std::cout << "Doing something #" << counter.impl << '\n'; +} + +} + +int main() { + try { + Counter counter; + std::array workers; + for (auto& worker : workers) { + worker = std::thread{&do_something, std::ref(counter)}; + } + for (auto& worker : workers) { + worker.join(); + } + } catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + return 1; + } + return 0; +} -- cgit v1.3