-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread1.cpp
45 lines (34 loc) · 920 Bytes
/
thread1.cpp
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
40
41
42
43
44
45
#include <iostream>
#include <cstdlib>
#include <thread>
using namespace std;
#define NUM_THREADS 5
void foo()
{
for (int i = 0; i < 10; i++)
{
cout << "printing " << i << "elephants from foo thread\n";
cout << "going to sleep for 5 seconds\n";
_sleep(5*1000);
}
}
void bar(int x)
{
for (int i = 0; i < x; i++)
{
cout << "printing " << i << "birds from bar thread\n";
cout << "going to sleep for 3 seconds\n";
_sleep(3*1000);
}
}
int main()
{
std::thread first(foo); // spawn new thread that calls foo()
std::thread second(bar, 5); // spawn new thread that calls bar(0)
std::cout << "main, foo and bar now execute concurrently...\n";
// synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes
std::cout << "foo and bar completed.\n";
return 0;
}