aboutsummaryrefslogtreecommitdiff
path: root/operating-system-challenge/1/main.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-03-17 17:15:19 +0800
committercrupest <crupest@outlook.com>2021-03-17 17:15:19 +0800
commit65ac04604220f3dff8465cc8465ec469049529e9 (patch)
tree0e0b5fcd296e0fb742229f56cce8d5f18ca3840f /operating-system-challenge/1/main.cpp
parent9ee5f6967ebb049eb610cfc411e647081b096d77 (diff)
downloadlife-65ac04604220f3dff8465cc8465ec469049529e9.tar.gz
life-65ac04604220f3dff8465cc8465ec469049529e9.tar.bz2
life-65ac04604220f3dff8465cc8465ec469049529e9.zip
Add OS challenges.
Diffstat (limited to 'operating-system-challenge/1/main.cpp')
-rw-r--r--operating-system-challenge/1/main.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/operating-system-challenge/1/main.cpp b/operating-system-challenge/1/main.cpp
new file mode 100644
index 0000000..0f780ef
--- /dev/null
+++ b/operating-system-challenge/1/main.cpp
@@ -0,0 +1,33 @@
+// Remember not to use O2 optimization!
+
+#include <thread>
+#include <vector>
+
+int main() {
+ std::vector<std::thread> threads;
+
+ // Because my CPU has 4-core and 8-threads. I open 8 threads.
+ for (int i = 0; i < 8; i++) {
+ std::thread t([] {
+ int i;
+ int j;
+ int k;
+ double d;
+ while (true) {
+ i = i + i;
+ j = j - j;
+ k = k * k;
+ d = d + d;
+ i = i & i;
+ j = j | j;
+ }
+ });
+ threads.push_back(std::move(t));
+ }
+
+ for (auto &t : threads) {
+ t.join();
+ }
+
+ return 0;
+}