C++11 this_thread::sleep_for(10)
来源:互联网 发布:vr应用领域 数据科学 编辑:程序博客网 时间:2024/06/06 01:49
std::this_thread::sleep_for
template <class Rep, class Period> void sleep_for (const chrono::duration<Rep,Period>& rel_time);
阻塞当前线程rel_time的时间。
参考链接:http://www.cnblogs.com/jwk000/p/3560086.html一
The execution of the current thread is stopped until at least rel_time has passed from now. Other threads continue their execution.
从当前时间开始阻塞当前线程直到rel_time(一段时间)这段时间过去,其他线程依旧指向。
例子:
// thread::get_id / this_thread::get_id#include <iostream> // std::cout#include <thread> // std::thread, std::thread::id, std::this_thread::get_id#include <chrono> // std::chrono::secondsusing namespace std;void show(int n) { if (n==5){cout<<"start n=5"<<endl;this_thread::sleep_for(chrono::seconds(5));cout<<"sleep_for(chrono::seconds(5)) end"<<endl;} else{cout<<"This is not 5"<<endl;}}int main() {thread t(show,5);thread t1(show,100);t.join();t1.join();}运行截图:
Parameters
- rel_time
- The time span after which the calling thread shall resume its execution.
Note that multi-threading management operations may cause certain delays beyond this.
duration is an object that represents a specific relative time.
Return value
noneExample
12345678910111213141516
// this_thread::sleep_for example#include <iostream> // std::cout, std::endl#include <thread> // std::this_thread::sleep_for#include <chrono> // std::chrono::seconds int main() { std::cout << "countdown:\n"; for (int i=10; i>0; --i) { std::cout << i << std::endl; std::this_thread::sleep_for (std::chrono::seconds(1)); } std::cout << "Lift off!\n"; return 0;}
Output (after 10 seconds):
countdown:10987654321Lift off!
Exception safety
If the type of rel_time never throws exceptions (like the instantiations of duration in header <chrono>), this function never throws exceptions (no-throw guarantee).
—————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-4
于GDUT
——————————————————————————————————————————————————————————————————
- C++11 this_thread::sleep_for(10)
- boost::this_thread::sleep_for()死锁
- C++11 error: ‘sleep_for’ is not a member of ‘std::this_thread’
- C++11 this_thread::sleep_until(11)
- C++11 this_thread::get_id(9)
- C++11 this_thread::yeild(12)
- C++11 'yield' is not a member of 'std::this_thread'
- std :: this_thread
- std::this_thread::sleep_until
- std::this_thread::yield()使用理解
- 通过sleep_for来使线程睡眠一定的时间
- 使用std::mutex(注意其中get_id()和sleep_for()
- Break the infinity thread loop using boost::this_thread::interruption_point()
- Boost编程之--慎用线程的this_thread::yield()方法
- 尽量不要在生产环境代码中使用boost::this_thread::sleep
- C(11)
- 11C
- C(10)
- CodeForces-116A-Tram
- Java 多线程间的通讯
- 今天才开通CSDN博客,惭愧,菜鸟上路啦
- Java动态数组ArrayList
- C,C++算法细节记录(不断增加中)
- C++11 this_thread::sleep_for(10)
- iOS学习笔记——获取手机信息(UIDevice、NSBundle、NSLocale)
- 二叉树(4)非递归法遍历二叉树
- DOM学习笔记(五)
- HDU 1160 FatMouse's Speed
- 深入理解Android
- R语言兴起与SAS,SPSS
- memcpy
- do...while(0)在宏定义中的用法