From 5018c2b40009258ed0691cbbb1dcc4d8ac4a809b Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 10 Jun 2021 11:55:27 +0800 Subject: import(life): ... --- .../InterlockedAvoidDataRaceDemo.cpp | 28 ++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp (limited to 'works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp') diff --git a/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp b/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp new file mode 100644 index 0000000..91d6d50 --- /dev/null +++ b/works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp @@ -0,0 +1,28 @@ +#include "Interlocked.hpp" +#include "Thread.h" + +#include + +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; +} -- cgit v1.2.3