aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/foo.cpp
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2020-11-19 03:08:27 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2020-11-19 05:52:39 +0300
commit92327f87384c357264454a98c83aae733a6b6495 (patch)
treea1baa841ad1a8691088b96b0a57d4c223b994515 /foo.cpp
downloadsetup-mingw-92327f87384c357264454a98c83aae733a6b6495.tar.gz
setup-mingw-92327f87384c357264454a98c83aae733a6b6495.zip
initial commit
Diffstat (limited to '')
-rw-r--r--foo.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/foo.cpp b/foo.cpp
new file mode 100644
index 0000000..a7891bb
--- /dev/null
+++ b/foo.cpp
@@ -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;
+}