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 by 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::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();	
}
运行截图:

C++11 this_thread::sleep_for(10)_第1张图片

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

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; }
Edit & Run


Output (after 10 seconds):
countdown: 10 9 8 7 6 5 4 3 2 1 Lift 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:[email protected]

2014-9-4

于GDUT

——————————————————————————————————————————————————————————————————





你可能感兴趣的:(thread,C++11)