diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-12-25 04:45:24 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2021-01-01 02:23:00 +0300 |
commit | 8d8d2c1c0577d16b00cf27e29212cf0a1737ae7f (patch) | |
tree | b11a27a6e1fd19d0b5707ca3efed9610b1fa184b /foo.cpp | |
download | setup-clang-8d8d2c1c0577d16b00cf27e29212cf0a1737ae7f.tar.gz setup-clang-8d8d2c1c0577d16b00cf27e29212cf0a1737ae7f.zip |
initial commit
Diffstat (limited to '')
-rw-r--r-- | foo.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -0,0 +1,39 @@ +#include <array> +#include <cstddef> +#include <exception> +#include <functional> +#include <iostream> +#include <mutex> +#include <thread> + +namespace { + +struct Counter { + std::mutex mtx; + std::size_t impl = 0; +}; + +void do_something(Counter& counter) { + std::lock_guard<std::mutex> lck{counter.mtx}; + ++counter.impl; + std::cout << "Doing something #" << counter.impl << '\n'; +} + +} + +int main() { + try { + Counter counter; + std::array<std::thread, 3> 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; +} |