`
阿尔萨斯
  • 浏览: 4106712 次
社区版块
存档分类
最新评论

C++11 this_thread::sleep_for(10)

 
阅读更多
原文地址:http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
function
<thread>

std::this_thread::sleep_for

template <class Rep, class Period>
  void sleep_for (const chrono::duration<Rep,Period>& rel_time);
Sleep for time span
Blocks execution of the calling thread during the span of time specified byrel_time.

阻塞当前线程rel_time的时间。

参考链接:http://www.cnblogs.com/jwk000/p/3560086.html


The execution of the current thread is stopped until at leastrel_timehas 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::seconds
using 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.
durationis an object that represents a specificrelative time.
线程阻塞的时间。

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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:
10
9
8
7
6
5
4
3
2
1
Lift off!

Exception safety

If the type ofrel_timenever throws exceptions (like the instantiations ofdurationin 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 线程休眠函数

    C++ 11之前并未提供专门的休眠函数。c语言的sleep、usleep其实都是系统提供的函数,不同的系统函数的功能还有些差异。 在Windows系统中,sleep...std::this_thread::sleep_for std::this_thread::sleep_untill 1. 让线

    cpp-perf:用于测量 C++ 代码性能的小型头文件库

    std::this_thread::sleep_for ( perf::milliseconds ( 20 )); perf::stop (); return 0 ;} 输出: 20 ms在线测量# include &lt; cpp&gt;int main () { PERF_START (); std::this_thread::sleep_for ( perf::milliseconds ...

    SleepTest.zip

     2、C++11的this_thread::sleep_for以及timeBeginPeriod调整定时器精度两个方式  3、socket连接的select的方式  4、多媒体时钟QueryPerformanceCounter的微秒级方式  5、MsgWaitForMultipleObjectsEx

    profc:简单,仅标头的 C++ 内部分析器

    std::this_thread::sleep_for ( std::chrono::milliseconds ( 1 )); } int main () { Foo (); Foo (); return 0 ; } 宏将自动开始测量并以示波器结束。 程序运行后,结果将打印到标准输出。 $ ./a.out -----...

    EurekaLog_7.5.0.0_Enterprise

    11)..Added: Support for detection of Windows 10 updates 12)..Added: OS edition detection 13)..Added: "User" and "Session" columns to processes list, processes list is also sorted by session first 14)....

    Linux多线程服务端编程:使用muduo C++网络库

    《Linux多线程服务端编程:使用muduo C++网络库》主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread。...

    海康卫视开发dll文件

    public int buffNum = 10; public int bytesReturned; public int channel = 1; public int channelCount = 5; // 共8个通道 public int channelStart = 1; /// /// 客户端信息结构 /// public ...

    Java测试题2答案

    DEF A notify() B wait() C notifyAll() D sleep() E.yield() F.synchronized(this) 7.构造BufferedInputStream的合适参数是哪个? AC A BufferedInputStream B BufferedOutputStream ...

    千方百计笔试题大全

    189、Can a Java Thread be started from Servlet class, and what will be the implications? 45 190、What is HTTP Session tracking and why is it important? 45 191、What is session management, and how is ...

    超级有影响力霸气的Java面试题大全文档

    sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。调用sleep不会释放对象锁。 wait是Object类的方法,对此对象调用wait方法导致本线程...

    汪文君高并发编程实战视频资源全集

    │ 高并发编程第一阶段10讲、Thread构造函数StackSize详细讲解.mp4 │ 高并发编程第一阶段11讲、Thread构造函数StackSize详细讲解-续.mp4 │ 高并发编程第一阶段12讲、Daemon线程的创建以及使用场景分析.mp4 │ ...

    汪文君高并发编程实战视频资源下载.txt

    │ 高并发编程第一阶段10讲、Thread构造函数StackSize详细讲解.mp4 │ 高并发编程第一阶段11讲、Thread构造函数StackSize详细讲解-续.mp4 │ 高并发编程第一阶段12讲、Daemon线程的创建以及使用场景分析.mp4 │ ...

    java面试宝典

    189、Can a Java Thread be started from Servlet class, and what will be the implications? 45 190、What is HTTP Session tracking and why is it important? 45 191、What is session management, and how is ...

    java 面试题 总结

    sleep是线程类(Thread)的方法,导致此线程暂停执行指定时间,给执行机会给其他线程,但是监控状态依然保持,到时后会自动恢复。调用sleep不会释放对象锁。 wait是Object类的方法,对此对象调用wait方法导致本线程...

Global site tag (gtag.js) - Google Analytics