c++多線程基礎2(命名空間 this_thread)
std::this_thread::yield:
定義於頭文件 <thread>
函數原型:void yield() noexcept;
此函數的準確性為依賴於實現,特別是使用中的 OS 調度器機制和系統狀態。例如,先進先出實時調度器( Linux 的 SCHED_FIFO
)將懸掛當前線程並將它放到準備運行的同優先級線程的隊列尾(而若無其他線程在同優先級,則 yield
無效果)
代碼:
1 #include <iostream> 2 #include <thread> 3 #include <chrono> 4 using namespaceView Codestd; 5 6 void little_sleep(std::chrono::milliseconds us) { 7 auto start = std::chrono::high_resolution_clock::now(); 8 auto end = start + us; 9 do { 10 std::this_thread::yield();//讓出當前時間片 11 }while(std::chrono::high_resolution_clock::now() < end); 12 } 13 14 int main(void) { 15 auto start = std::chrono::high_resolution_clock::now();//獲取當前時間 16 17 little_sleep(std::chrono::milliseconds(100)); 18 19 auto elapsed = std::chrono::high_resolution_clock::now() - start;//計算執行 little_sleep 所用時間 20 21 cout << "waited fo " 22 << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count() //將 elapsed 時間周期轉化為 milliseconds 並輸出 23 << " milliseconds\n"; 24 25 // 輸出: 26 // waited fo 100 milliseconds 27 28 return 0; 29 }
std::this_thread::get_id:
定義於頭文件 <thread>
函數原型:std::thread::id get_id() noexcept;
得到當前線程的 id
代碼:
1 #include <iostream> 2 #include <thread> 3 #include <chrono> 4 #include <mutex> 5 using namespace std; 6 7 std::mutex g_display_mutex; 8 9 void foo() { 10 auto this_id = std::this_thread::get_id(); 11 12 g_display_mutex.lock(); 13 cout << "thread" << this_id << " sleeping..." << endl; 14 g_display_mutex.unlock(); 15 16 std::this_thread::sleep_for(std::chrono::seconds(1)); 17 } 18 19 int main(void) { 20 std::thread t1(foo); 21 std::thread t2(foo); 22 23 t1.join(); 24 t2.join(); 25 26 // 輸出: 27 // thread2 sleeping... 28 // thread3 sleeping... 29 30 return 0; 31 }View Code
std::this_thread::sleep_for:
定義於頭文件 <thread>
函數原型:
template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );
阻塞當前線程執行,以至少為指定的 sleep_duration
。
此函數可能阻塞長於 sleep_duration
,因為調度或資源爭議延遲。
標準庫建議用穩定時鐘度量時長。若實現用系統時間代替,則等待時間亦可能對始終調節敏感
異常:任何時鐘、 time_point 或 duration 在執行間拋出的異常(標準庫提供的時鐘、時間點和時長決不拋出)
代碼:
1 #include <iostream> 2 #include <chrono> 3 #include <thread> 4 using namespace std; 5 6 int main(void) { 7 cout << "hello waiter" << endl; 8 9 auto start = std::chrono::high_resolution_clock::now(); 10 std::this_thread::sleep_for(std::chrono::seconds(2)); 11 auto end = std::chrono::high_resolution_clock::now(); 12 13 std::chrono::duration<double, std::milli> elapsed = end - start; 14 cout << "waited " << elapsed.count() << " ms" << endl; 15 16 // 輸出: 17 // hello waiter 18 // waited 2001.44 ms 19 20 return 0; 21 }View Code
std::this_thread::sleep_until:
定義於頭文件 <thread>
template< class Clock, class Duration >
void sleep_until( const std::chrono::time_point<Clock,Duration>& sleep_time );
阻塞當前線程,直至抵達指定的 sleep_time
。
使用聯傾向於 sleep_time
的時鐘,這表示時鐘調節有影響。從而在調用時間點後,阻塞的時長可能小於,但不會多於 sleep_time - Clock::now() 。函數亦可能阻塞長於抵達 sleep_time
之後,由於調度或資源爭議延遲
代碼:
1 #include <iostream> 2 #include <chrono> 3 #include <thread> 4 using namespace std; 5 6 int main(void) { 7 auto start = std::chrono::high_resolution_clock::now(); 8 std::this_thread::sleep_until(start + std::chrono::seconds(2)); 9 auto end = std::chrono::high_resolution_clock::now(); 10 11 std::chrono::duration<double, std::milli> elapsed = end - start; 12 cout << "waited " << elapsed.count() << " ms" << endl; 13 14 // 輸出: 15 // waited 2001.42 ms 16 17 return 0; 18 }View Code
c++多線程基礎2(命名空間 this_thread)