aboutsummaryrefslogtreecommitdiff
path: root/works/life/operating-system-experiment/DataRaceDemo.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-06-10 11:12:25 +0800
committercrupest <crupest@outlook.com>2021-06-10 11:12:25 +0800
commitef593ba2d445af515214d63bfd3942be9d673467 (patch)
tree6f286d3c30af2a6c74a1a60047fa25bae09fd9db /works/life/operating-system-experiment/DataRaceDemo.cpp
parent93d8feccd79b35fc890fc8629d6da2c2baa57762 (diff)
downloadcrupest-ef593ba2d445af515214d63bfd3942be9d673467.tar.gz
crupest-ef593ba2d445af515214d63bfd3942be9d673467.tar.bz2
crupest-ef593ba2d445af515214d63bfd3942be9d673467.zip
import(life): ...
Diffstat (limited to 'works/life/operating-system-experiment/DataRaceDemo.cpp')
-rw-r--r--works/life/operating-system-experiment/DataRaceDemo.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/works/life/operating-system-experiment/DataRaceDemo.cpp b/works/life/operating-system-experiment/DataRaceDemo.cpp
new file mode 100644
index 0000000..9589eb3
--- /dev/null
+++ b/works/life/operating-system-experiment/DataRaceDemo.cpp
@@ -0,0 +1,27 @@
+#include "Thread.h"
+
+#include <iostream>
+
+int main() {
+ unsigned data = 0;
+
+ cru::Thread t1([&data] {
+ for (int i = 0; i < 100000; i++)
+ data += 10;
+ });
+
+ cru::Thread t2([&data] {
+ for (int i = 0; i < 100000; i++)
+ 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 == 200000 ? "correct" : "false") << '\n';
+
+ return 0;
+}