-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctional_thread.cpp
More file actions
81 lines (55 loc) · 1.86 KB
/
Copy pathfunctional_thread.cpp
File metadata and controls
81 lines (55 loc) · 1.86 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// questions
// 1. What do you understand by thread and give one example in C++?
// Answer:
// 0. In every application there is a default thread which is main() , in side with we create other threads
// 1. A thread is also known as lightweight process. Ideas is achieve parallelism by dividing a process into multiple threads.
// For example:
// a) The browser has multiple tabs that can be different threads.
// b) MS word must be using multiple threads. one thread to fromat the text, another thread to process inputs (spell checker)
// c) Visual Studio code editor would be using threading for auto completing the code. (Intellicence)
// WAYS TO CREATE THREADS IN C+=11
// 1. Function Pointers
// 2.Lambda Function
// 3.Functors
// 4/Member Functions
// 5. Static Member Function
#include<iostream>
#include<thread>
#include<chrono>
#include<algorithm>
using namespace std;
using namespace std :: chrono;
typedef unsigned long long ull;
ull OddSum=0;
ull EvenSum=0;
void findEven(ull start, ull end){
for(ull i=start;i<=end;i++){
if((i&1)==0){
EvenSum+=i;
}
}
}
void findOdd(ull start,ull end){
for(ull i=start;i<=end;i++){
if((i&1)==1){
OddSum+=i;
}
}
}
int main(){
auto startTime = high_resolution_clock::now();
ull start=0;
ull end = 1900000000;
std::thread t1(findEven,start,end); // thread need callable object which is given as "findEven,start,end";
std::thread t2(findOdd,start,end);
t1.join(); // this is to used to join with "main" thread;
t2.join();
// findOdd(start, end);
// findEven(start, end);
auto StopTime = high_resolution_clock::now();
auto duration= duration_cast<microseconds>(StopTime-startTime);
cout<<"OddSum : "<< OddSum<<endl;
cout<<"EvenSum : "<< EvenSum<<endl;
cout<<"Sec : "<<duration.count()/1000000<<endl;
return 0;
}