/
githubmirror
/
CPlusPlusThings
Обзор
Документация
Войти
/
githubmirror
/
CPlusPlusThings
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
concurrency/Threading_In_CPlusPlus/1.thread/thread.cpp
56 строк
1 KB
Light-City
update
03 мар 2020, 05:00
03 мар 2020, 05:00
16c12a3
Код
Авторство
О чём код?
// // 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; }