From 7c15bf3d194b92af5f6891187359286adc9e961f Mon Sep 17 00:00:00 2001
From: Erik Strand <erik.strand@cba.mit.edu>
Date: Sun, 25 Nov 2018 20:29:22 -0500
Subject: [PATCH] Add example of atomic variables

---
 atomic.cpp | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
 create mode 100644 atomic.cpp

diff --git a/atomic.cpp b/atomic.cpp
new file mode 100644
index 0000000..87c50bf
--- /dev/null
+++ b/atomic.cpp
@@ -0,0 +1,27 @@
+#include <array>
+#include <thread>
+#include <iostream>
+
+int main() {
+    constexpr auto n_threads = 10u;
+    std::atomic<int> counter = {0};
+    std::array<std::thread, n_threads> threads;
+
+    const auto increment = [](std::atomic<int>& i) {
+        // We don't care what order the the threads perform their increment operations.
+        i.fetch_add(1, std::memory_order_relaxed);
+    };
+
+    for (auto i = 0u; i < n_threads; ++i) {
+        threads[i] = std::thread(increment, std::ref(counter));
+    }
+
+    for (auto i = 0u; i < n_threads; ++i) {
+        threads[i].join();
+    }
+
+    std::cout << "Final value: " << counter << std::endl;
+
+    return 0;
+}
+
-- 
GitLab