diff options
author | crupest <crupest@outlook.com> | 2021-06-10 11:55:27 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-10 11:55:27 +0800 |
commit | 5018c2b40009258ed0691cbbb1dcc4d8ac4a809b (patch) | |
tree | 1c4269b73b0473f47b98ca260676dbec1460791f /works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp | |
parent | 06c91dd3a3d17bd1f884c7e322fd7701a95ce7a9 (diff) | |
download | crupest-5018c2b40009258ed0691cbbb1dcc4d8ac4a809b.tar.gz crupest-5018c2b40009258ed0691cbbb1dcc4d8ac4a809b.tar.bz2 crupest-5018c2b40009258ed0691cbbb1dcc4d8ac4a809b.zip |
import(life): ...
Diffstat (limited to 'works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp')
-rw-r--r-- | works/life/operating-system-experiment/InterlockedAvoidDataRaceDemo.cpp | 28 |
1 files changed, 28 insertions, 0 deletions
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 <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; +} |