aboutsummaryrefslogtreecommitdiff
path: root/store/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'store/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp')
-rw-r--r--store/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/store/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp b/store/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp
new file mode 100644
index 0000000..91d6d50
--- /dev/null
+++ b/store/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp
@@ -0,0 +1,28 @@
+#include "Interlocked.hpp"
+#include "Thread.h"
+
+#include <iostream>
+
+int main() {
+ volatile long long data = 0;
+
+ cru::Thread t1([&data] {
+ for (int i = 0; i < 100000; i++)
+ cru::CruInterlockedAdd(&data, 10);
+ });
+
+ cru::Thread t2([&data] {
+ for (int i = 0; i < 100000; i++)
+ cru::CruInterlockedAdd(&data, 10);
+ });
+
+ std::cout << "Created thread: " << t1.GetNativeID() << '\n';
+ std::cout << "Created thread: " << t2.GetNativeID() << '\n';
+
+ t1.Join();
+ t2.Join();
+ std::cout << "Answer is " << data << ", which is "
+ << (data == 2000000 ? "correct" : "false") << '\n';
+
+ return 0;
+}