您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“C++ sleep()和usleep()的區(qū)別是什么”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
sleep() 和 usleep()主要區(qū)別前者單位為秒,后者為微妙(*1e6)
代碼如下:設(shè)置一個(gè)時(shí)間參數(shù),分別向sleep()和usleep()方法傳入該參數(shù),打印sleep和usleep前后的系統(tǒng)時(shí)間戳
#include <time.h> #include <stdio.h> #include <iostream> #include <unistd.h> using namespace std; int main(int argc, char *argv[]){ int sleep_time = 100; cout << "sleep before time: " << time(NULL) << endl; sleep(sleep_time); cout << "sleep after time: " << time(NULL) << endl; cout << "======================" << endl; cout << "usleep before time: " << time(NULL) << endl; usleep(sleep_time); cout << "usleep after time: " << time(NULL) << endl; }
運(yùn)行結(jié)果:
sleep before time: 1560257692
sleep after time: 1560257792
======================
usleep before time: 1560257792
usleep after time: 1560257792
從運(yùn)行結(jié)果來(lái)看,sleep()方法執(zhí)行前后的時(shí)間戳相減正好是sleep_time大小,打印的時(shí)間戳是以秒為單位的,所以sleep()方法將程序掛起使用的單位是秒級(jí)別的;從usleep()執(zhí)行前后的時(shí)間戳來(lái)看,感覺(jué)上程序并沒(méi)有被掛起,這是因?yàn)閡sleep()方法將程序掛起的使用的單位是微秒級(jí)別的(1,000,000 微秒 = 1秒)所以程序看起來(lái)并沒(méi)有被掛起,下面我們將打印的時(shí)間戳改為微秒級(jí)別的看看。
可以通過(guò)<sys/time.h>庫(kù)中的gettimeofday()方法來(lái)拿到當(dāng)前系統(tǒng)的微秒級(jí)別時(shí)間。
#include <stdio.h> #include <iostream> #include <unistd.h> #include <sys/time.h> using namespace std; int main(int argc, char *argv[]){ int sleep_time = 100; cout << "sleep before time: " << time(NULL) << endl; sleep(sleep_time); cout << "sleep after time: " << time(NULL) << endl; cout << "======================" << endl; cout << "usleep before time: " << time(NULL) << endl; usleep(sleep_time); cout << "usleep after time: " << time(NULL) << endl; cout << "======================" << endl; struct timeval tv; gettimeofday(&tv,NULL); cout << "not usleep before time: " << tv.tv_sec<<"s,"<<tv.tv_usec<<"微秒"<<endl; usleep(0); gettimeofday(&tv,NULL); cout << "not usleep after time: " << tv.tv_sec<<"s,"<<tv.tv_usec<<"微秒"<<endl; cout << "======================" << endl; gettimeofday(&tv,NULL); cout << "usleep before time: " << tv.tv_sec<<"s,"<<tv.tv_usec<<"微秒"<<endl; usleep(sleep_time); gettimeofday(&tv,NULL); cout << "usleep after time: " << tv.tv_sec<<"s,"<<tv.tv_usec<<"微秒"<<endl; }
運(yùn)行結(jié)果
sleep before time: 1560260201
sleep after time: 1560260301
======================
usleep before time: 1560260301
usleep after time: 1560260301
======================
not usleep before time: 1560260301s,491772微秒
not usleep after time: 1560260301s,491948微秒
======================
usleep before time: 1560260301s,491962微秒
usleep after time: 1560260301s,492191微秒
從使用usleep(0)和使用了usleep(100)方法來(lái)看,執(zhí)行前后的時(shí)間差差不多相差100,從而可以看出usleep()是以微秒級(jí)別為單位的。
“C++ sleep()和usleep()的區(qū)別是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。