forked from mpavezb/cpp_concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_thread_parameters.cpp
More file actions
39 lines (31 loc) · 936 Bytes
/
04_thread_parameters.cpp
File metadata and controls
39 lines (31 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <chrono>
#include <cstdio>
#include <thread>
void foo_by_value(int x, int y) { printf("%d + %d = %d\n", x, y, x + y); }
void foo_by_ref(int const &n) {
while (true) {
printf("N = %d\n", n);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
int main() {
// ============================================
// Pass by Value
// ============================================
int x = 10;
int y = 5;
std::thread thread_by_value(foo_by_value, x, y);
thread_by_value.join();
// ============================================
// Pass by Reference
// ============================================
int N = 10;
printf("N = %d\n", N);
// Explicitly wrap as reference, otherwise the variable is passed by value.
std::thread thread_by_ref(foo_by_ref, std::ref(N));
std::this_thread::sleep_for(std::chrono::seconds(5));
N = 15;
printf("N = %d\n", N);
thread_by_ref.join();
return 0;
}