Created
February 24, 2020 05:19
-
-
Save imsjz/61c694e058b07418ced7e27583b64be1 to your computer and use it in GitHub Desktop.
c++11新特性:可以用库函数创建平台无关的多线程
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <thread> | |
#include <chrono> // 时间 | |
#include <algorithm> | |
using namespace std; | |
using namespace std::chrono; | |
typedef unsigned long ul; | |
ul OddSum = 0; | |
ul EvenSum = 0; | |
void findOdd(ul start, ul end) { | |
for(ul i = start; i <= end; ++i) { | |
if((i & 1) == 1) { | |
OddSum += i; | |
} | |
} | |
} | |
void findEven(ul start, ul end) { | |
for(ul i = start; i <= end; ++i) { | |
if((i & 1) == 0) { | |
EvenSum += i; | |
} | |
} | |
} | |
// 创建线程的5中方法 | |
namespace sanjay2 { | |
// 1. Function pointer | |
void fun(int x) { | |
while(x-- > 0) { | |
cout << x << endl; | |
} | |
} | |
void test1() { | |
std::thread t(fun, 10); | |
t.join(); | |
} | |
// 2. lambda function | |
void test2() { | |
auto fun = [](int x) { | |
while(x-- > 0) { | |
cout << x << endl; | |
} | |
}; | |
std::thread t(fun, 11); | |
//更紧凑的写法 | |
std::thread t2([](int x) { | |
while (x-- > 0) { | |
cout << x << endl; | |
} | |
}, 12); // 这样会和t线程输出信息混合,输出有点乱 | |
t.join(); | |
t2.join(); | |
} | |
// 3. functor (function object) | |
class Base{ | |
public: | |
void operator()(int x) { | |
while (x-- > 0) { | |
cout << x << endl; | |
} | |
} | |
}; | |
void test3() { | |
std::thread t(Base(), 14); | |
t.join(); | |
} | |
// 4. non-static member function | |
class Base1{ | |
public: | |
void run(int x) { | |
while (x-- > 0) { | |
cout << x << endl; | |
} | |
} | |
static void fun(int x) { | |
while (x-- > 0) { | |
cout << x << endl; | |
} | |
} | |
}; | |
void test4() { | |
Base1 b; | |
std::thread t(&Base1::run, &b, 15); | |
t.join(); | |
} | |
// 5. static member function | |
void test5() { | |
std::thread t(&Base1::fun, 16); | |
t.join(); | |
} | |
} | |
// try_lock | |
#include <mutex> | |
namespace sanjay3 { | |
int counter = 0; | |
std::mutex mtx; | |
void increaseTheCounter100000Time() { | |
for(int i = 0; i < 100000; ++i) { | |
// if(mtx.try_lock()) { | |
// ++counter; | |
// mtx.unlock(); | |
// } | |
//如果换成 | |
mtx.lock(); | |
++counter; | |
mtx.unlock(); | |
} | |
} | |
void test() { | |
std::thread t1(increaseTheCounter100000Time); | |
std::thread t2(increaseTheCounter100000Time); | |
t1.join(); | |
t2.join(); | |
cout << "counter could increase upto " << counter << endl; | |
} | |
} | |
int main() { | |
/* | |
ul start = 0, end = 19000000000; | |
auto start_time = high_resolution_clock::now(); | |
// findOdd(start, end); | |
// findEven(start, end); | |
// 用线程来执行 | |
thread t1(findOdd, start, end); | |
thread t2(findEven, start, end); | |
t1.join(); | |
t2.join(); | |
auto end_time = high_resolution_clock::now(); | |
auto duration = duration_cast<microseconds>(end_time - start_time); | |
cout << "OddSum : " << OddSum << endl; | |
cout << "EvenSum : " << EvenSum << endl; | |
cout << "Duration: " << duration.count() / 1000000 << endl; | |
*/ | |
// sanjay2::test5(); | |
sanjay3::test(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment