forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.cpp
56 lines (42 loc) · 1.06 KB
/
thread.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
//
// Created by light on 20-1-31.
//
#include <iostream>
#include <thread>
#include <chrono>
using namespace std::chrono;
using namespace std;
/**
* 1.普通函数指针
* 2.Lambda函数
* 3.Functors
* 4.非静态成员函数
* 5.静态成员函数
*/
using ull = unsigned long long;
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() {
ull start = 0, end = 1900000000;
auto startTime = high_resolution_clock::now();
std::thread t1(findEven,start,end);
std::thread t2(findOdd,start,end);
t1.join();
t2.join();
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;
}