網站首頁 編程語言 正文
1、stl方式
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
或std::this_thread::sleep_for(std::chrono::seconds(3));
(1)std::this_thread::yield (): 線程調用該方法時,主動讓出 CPU,并且不參與 CPU 的本次調度,從而讓其他線程有機會運行。在后續的調度周期里再參與 CPU 調度。這是主動放棄 CPU 的方法接口。
(2)std::sleep_for ():線程調用該方法時,同樣會讓出 CPU,并且休眠一段時間,從而讓其他線程有機會運行。等到休眠結束時,才參與 CPU 調度。這也是主動放棄 CPU 的方法。
兩者的不同很明顯,yield () 方法讓出 CPU 的時間是不確定的,并且以 CPU 調度時間片為單位。而 sleep_for () 讓出 CPU 的時間是固定的。
yield () 的實現依賴于操作系統 CPU 調度策略,在不同的操作系統或者同一個操作系統的不同調度策略下,表現也可能是不同的。
yield簡單示例如下
#include <iostream>
#include <thread>
int main(){
std::thread thread1([]{
while (true) {
std::cout<<"myThread1"<<std::endl;
std::this_thread::yield();
}
});
std::thread thread2([]{
while (true){
std::cout<<"myThread2"<<std::endl;
std::this_thread::yield();
}
});
if (thread1.joinable())
thread1.join();
if (thread2.joinable())
thread2 .join();
return 0;
}
2、用boost實現, 沒有用過
boost::this_thread::sleep( boost::posix_time::seconds(3) );
boost::this_thread::sleep( boost::posix_time::milliseconds(3000) );
3、sleep
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32
void sleepcp(int milliseconds) // 跨平臺 sleep 函數
{
#ifdef _WIN32
Sleep(milliseconds);//釋放cp
#else
usleep(milliseconds * 1000);
#endif // _WIN32
}
知識補充
c++中實現sleep的三種方式(跨平臺)
1、stl方式
//by 鳥哥 用stl實現sleep
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
int main(){
cout<<"sleep前"<<endl;
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
cout<<"sleep后"<<endl;
}
運行結果:
sleep前
sleep后
2、用boost實現
//by 鳥哥 用boost實現sleep
#include <iostream>
#include <boost/thread/thread.hpp>
using namespace std;
int main()
{
cout<<"sleep前"<<endl;
boost::this_thread::sleep( boost::posix_time::seconds(3) );
cout<<"3秒后"<<endl;
boost::this_thread::sleep( boost::posix_time::milliseconds(3000) );
cout<<"3秒后"<<endl;
return 0;
}
運行結果:
sleep前
3秒后
3秒后
3、sleep
//by 鳥哥 實現sleep
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32
using namespace std;
void sleepcp(int milliseconds) // 跨平臺 sleep 函數
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif // _WIN32
}
int main()
{
cout << "sleep前" << endl;
sleepcp(3000);
cout << "3秒后" << endl;
}
運行結果:
sleep前
3秒后
原文鏈接:https://blog.csdn.net/woquNOKIA/article/details/128411702
相關推薦
- 2022-09-25 spring如何解決循環依賴
- 2022-04-21 Python?數據類型中的字符串和數字_python
- 2022-07-19 Python數據分析之NumPy常用函數使用詳解_python
- 2023-09-17 POM文件中使用<exclusions>解決jar沖突問題
- 2022-03-11 fatal error LNK1120: 1 個無法解析的外部命令 的解決辦法
- 2022-10-02 React函數組件和類組件的區別及說明_React
- 2024-03-24 IDEA配置熱啟動
- 2021-12-12 c++虛函數與虛函數表原理_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支