forked from mpavezb/cpp_concurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_ownership.cpp
More file actions
28 lines (20 loc) · 730 Bytes
/
06_ownership.cpp
File metadata and controls
28 lines (20 loc) · 730 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
#include <cstdio>
#include <thread>
void foo() { printf("Hello from foo.\n"); }
void bar() { printf("Hello from bar.\n"); }
int main() {
std::thread thread_1(foo);
std::thread thread_2;
// Copy Disabled.
// std::thread thread_2 = thread_1;
// Move Enabled
std::thread thread_3(std::move(thread_1)); // move ctor with named variable
std::thread thread_4(std::thread(bar)); // move ctor with tmp variable
thread_1 = std::move(thread_3); // move assgn of named variable
thread_2 = std::thread(bar); // move assgn of tmp variable
// Reassign unjoined thread will result in std::terminate!
// thread_3 = std::move(thread_1);
thread_1.join();
thread_2.join();
return 0;
}