mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-02-04 11:03:58 +08:00
update
This commit is contained in:
56
Threading_In_CPlusPlus/1.thread/thread.cpp
Normal file
56
Threading_In_CPlusPlus/1.thread/thread.cpp
Normal file
@@ -0,0 +1,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;
|
||||
}
|
||||
Reference in New Issue
Block a user