詳解C++11 線程休眠函數(shù)
C++ 11之前并未提供專門的休眠函數(shù)。c語言的sleep、usleep其實(shí)都是系統(tǒng)提供的函數(shù),不同的系統(tǒng)函數(shù)的功能還有些差異。
在Windows系統(tǒng)中,sleep的參數(shù)是毫秒。
sleep(2*1000); //sleep for 2 seconds
在類Unix系統(tǒng)中,sleep()函數(shù)的單位是秒。
sleep(2); //sleep for 2 seconds
從C++11開始,中C++標(biāo)準(zhǔn)庫提供了專門的線程休眠函數(shù),使得你的代碼可以獨(dú)立于不同的平臺(tái)。
std::this_thread::sleep_for std::this_thread::sleep_untill
1. 讓線程休眠一段時(shí)間
std::this_thread::sleep_for用于Block當(dāng)前線程一段時(shí)間。
template< class Rep, class Period > void sleep_for( const std::chrono::duration<Rep, Period>& sleep_duration );
sleep的時(shí)間間隔從納秒到小時(shí)都有具體的定義。
std::chrono::nanoseconds std::chrono::microseconds std::chrono::milliseconds std::chrono::seconds std::chrono::minutes std::chrono::hours
比如我們想要一個(gè)線程休眠100ms。
std::this_thread::sleep_for(std::chrono::milliseconds(100));
我們想要一個(gè)線程休眠1分鐘:
std::this_thread::sleep_for(std::chrono::minutes(1));
完整的代碼示例:
#include <iostream> #include <chrono> #include <thread> int main() { std::cout << "Hello waiter\n" << std::flush; auto start = std::chrono::high_resolution_clock::now(); std::this_thread::sleep_for(std::chrono::milliseconds(2000)); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double, std::milli> elapsed = end-start; std::cout << "Waited " << elapsed.count() << " ms\n"; }
輸出:
Hello waiter
Waited 2000.12 ms
2. 休眠直至到一個(gè)時(shí)間點(diǎn)
template< class Clock, class Duration > void sleep_until( const std::chrono::time_point<Clock,Duration>& sleep_time )
sleep_until會(huì)阻塞當(dāng)前線程直至未來某個(gè)時(shí)間點(diǎn)到達(dá)。
#include <iostream> #include <thread> #include <chrono> // Print Current Time void print_time_point(std::chrono::system_clock::time_point timePoint) { std::time_t timeStamp = std::chrono::system_clock::to_time_t(timePoint); std::cout << std::ctime(&timeStamp) << std::endl; } void threadFunc() { std::cout<<"Current Time :: "; // Print Current Time print_time_point(std::chrono::system_clock::now()); // create a time point pointing to 10 second in future std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now() + std::chrono::seconds(10); std::cout << "Going to Sleep Until :: "; print_time_point(timePoint); // Sleep Till specified time point // Accepts std::chrono::system_clock::time_point as argument std::this_thread::sleep_until(timePoint); std::cout<<"Current Time :: "; // Print Current Time print_time_point(std::chrono::system_clock::now()); } int main() { std::thread th(&threadFunc); th.join(); return 0; }
程序輸出:
Current Time :: Sun Oct 11 02:57:38 2020
Going to Sleep Until :: Sun Oct 11 02:57:48 2020
Current Time :: Sun Oct 11 02:57:48 2020
參考材料
1.https://baike.baidu.com/item/sleep%E5%87%BD%E6%95%B0/6735027
2.https://thispointer.com/how-to-put-a-thread-to-sleep-in-c11-sleep_for-sleep_until/
以上就是詳解C++11 線程休眠函數(shù)的詳細(xì)內(nèi)容,更多關(guān)于C++11 線程休眠函數(shù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++ 動(dòng)態(tài)內(nèi)存分配詳解(new/new[]和delete/delete[])
這篇文章主要介紹了C++ 動(dòng)態(tài)內(nèi)存分配詳解(new/new[]和delete/delete[]),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理解析
這篇文章主要介紹了虛函數(shù)表-C++多態(tài)的實(shí)現(xiàn)原理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02C++11新特性“=default”,“=delete”的使用
=default、=delete 是C++11的新特性,分別為:顯式缺省(告知編譯器生成函數(shù)默認(rèn)的缺省版本)和顯式刪除(告知編譯器不生成函數(shù)默認(rèn)的缺省版本),本文就來介紹一下如何使用2021-05-05C++保存txt文件實(shí)現(xiàn)方法代碼實(shí)例
這篇文章主要介紹了C++保存txt文件實(shí)現(xiàn)方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11c語言根據(jù)用戶輸入的出生年份并計(jì)算出當(dāng)前年齡
這篇文章主要介紹了c語言根據(jù)用戶輸入的出生年份并計(jì)算出當(dāng)前年齡,需要的朋友可以參考下2023-03-03