-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadCpp.cpp
More file actions
38 lines (28 loc) · 835 Bytes
/
ThreadCpp.cpp
File metadata and controls
38 lines (28 loc) · 835 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
#include <iostream> // std::cout
#include <thread> // std::thread
void foo()
{
for (int i = 0; i < 1500000000; i++);
}
void bar(int x)
{
for (int i = 0; i < 1500000000; i++);
}
using namespace std;
int main()
{
std::thread first(foo); // spawn new thread that calls foo()
std::thread second(bar, 0); // spawn new thread that calls bar(0)
std::thread third(foo);
std::thread fourth(foo);
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
third.join();
fourth.join();
std::cout << "foo and bar completed.\n";
cout << "Done in " << clock() * 1000 / CLOCKS_PER_SEC << endl;
getchar();
return 0;
}