類模板 template <class T> shared_future; template <class R&> shared_future<R&>; // specialization : T is a reference type (R&) template <> shared_future<void>; // specialization : T is void
成員函數 | 描述 |
---|---|
get | 當共享狀態就緒時,返回存儲在共享狀態中的值的引用(或引起其異常) |
valid | 檢查 shared_future 對象是否與共享狀態關聯 |
wait | 等待共享狀態準備就緒 |
wait_for | 等待共享狀態在 rel_time 指定的時間內準備就緒 |
wait_until | 等待共享狀態準備就緒,最多直到abs_time時間點 |
#include <iostream> #include <thread> #include <future> using namespace::std; int mythread() { cout << "mythread begin" << endl; this_thread::sleep_for(chrono::microseconds(5000)); cout << "mythread end" << endl; return 5; } int main() { cout << "main begin" << endl; future<int> result = async(launch::async, mythread); shared_future<int> result_s = result.share(); // 等價 // shared_future<int> result_s = async(launch::async, mythread); if (result_s.valid()) { cout << result_s.get() << endl; cout << result_s.get() << endl; cout << result_s.get() << endl; } cout << "main end" << endl; return 0; }
輸出:ios
main begin mythread begin mythread end 5 5 5 main end
類模板 template <class T> struct atomic;
測試1:非原子操做,無鎖編程
#include <iostream> #include <thread> using namespace::std; int g_sum = 0; void add() { for (uint32_t i=0; i<10000000; ++i) ++g_sum; } int main() { auto beginTime = clock(); thread t1(add); thread t2(add); t1.join(); t2.join(); auto endTime = clock(); cout << "time consuming : " << endTime - beginTime << endl; cout << "calculated value: " << g_sum << endl; return 0; }
輸出:[速度快,結果錯誤]多線程
time consuming : 47 calculated value: 10856025
測試2:非原子操做,有鎖async
#include <iostream> #include <thread> #include <mutex> using namespace::std; int g_sum = 0; mutex g_mutex; void add() { for (uint32_t i=0; i<10000000; ++i) { g_mutex.lock(); ++g_sum; g_mutex.unlock(); } } int main() { auto beginTime = clock(); thread t1(add); thread t2(add); t1.join(); t2.join(); auto endTime = clock(); cout << "time consuming : " << endTime - beginTime << endl; cout << "calculated value: " << g_sum << endl; return 0; }
輸出:[結果正確,速度慢]函數
time consuming : 571 calculated value: 20000000
測試3:原子操做測試
#include <iostream> #include <thread> #include <atomic> using namespace::std; atomic<int> g_sum {0}; void add() { for (uint32_t i=0; i<10000000; ++i) { ++g_sum; } } int main() { auto beginTime = clock(); thread t1(add); thread t2(add); t1.join(); t2.join(); auto endTime = clock(); cout << "time consuming : " << endTime - beginTime << endl; cout << "calculated value: " << g_sum << endl; return 0; }
輸出:[速度快,結果正確]ui
time consuming : 292 calculated value: 20000000
#include <iostream> #include <thread> #include <atomic> using namespace::std; atomic<bool> g_ifEnd {false}; void mythread() { cout << "mythread begin" << endl; chrono::microseconds dura(1000); while (!g_ifEnd) { cout << "mythread thread id :" << this_thread::get_id() << endl; this_thread::sleep_for(dura); } cout << "mythread begin" << endl; } int main() { cout << "main end" << endl; thread t1(mythread); this_thread::sleep_for(chrono::microseconds(5000)); g_ifEnd = true; t1.join(); cout << "main end" << endl; return 0; }
輸出:this
main end mythread begin mythread thread id :2 mythread begin main end