-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function Pointers - example.cpp
57 lines (40 loc) · 1.23 KB
/
Function Pointers - example.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
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include<thread>
#include<chrono>
#include<algorithm>
using namespace std;
using namespace std :: chrono;
typedef unsigned long long ull;
void oddSum(ull start, ull end){
ull sum = 0;
for(ull i = start; i<end; i++){
if(i % 2){
sum += i;
}
}
cout<<"odd sum = "<<sum<<endl;
}
void evenSum(ull start, ull end){
ull sum = 0;
for(ull i = start; i<end; i++){
if(i % 2 == 0){
sum += i;
}
}
cout<<"even sum = "<<sum<<endl;
}
int main()
{
ull end = 1900000000;
ull start = 0;
auto startTime = high_resolution_clock()::now();
thread t1(oddSum, start, end); // starts thread1 after reading this line and comes beak to main
thread t2(evenSum, start, end); //// starts thread2 after reading this line and comes beak to main
// join the created therads with main thread
t1.join(); // makes main thread wait till t1 finishes
t2.join(); // makes main thread wait till t2 finishes
auto stopTime = high_resolution_clock()::now();
auto duration = duration_cast<microseconds>(stopTime - startTime);
cout<<"time = "<<duration.count()/1000000<<endl;
return 0;
}