std::this_thread::sleep_for

std::this_thread::sleep_for

Defined in header <thread> - 定义于头文件 <thread>

1. std::this_thread::sleep_for

template <class Rep, class Period>
void sleep_for (const chrono::duration<Rep,Period>& rel_time);

阻塞当前线程执行,至少经过指定的 sleep_duration

此函数可能阻塞长于 sleep_duration,因为调度或资源争议延迟。

标准库建议用稳定时钟度量时长。若实现用系统时间代替,则等待时间亦可能对时钟调节敏感。

Sleep for time span - 睡眠一段时间

Blocks execution of the calling thread during the span of time specified by rel_time.
rel_time 指定的时间段内阻止调用线程的执行。

The execution of the current thread is stopped until at least rel_time has passed from now. Other threads continue their execution.
当前线程的执行将停止,直到从现在起至少经过 rel_time 为止。其他线程继续执行。

2. 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.
持续时间是代表特定相对时间的对象。

要睡眠的时长。

resume [rɪ'zjuːm]:n. 简历 v. 继续,重返

3. Return value

none

4. Examples

4.1 std::this_thread::sleep_for

//============================================================================
// Name        : std::this_thread::sleep_for
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#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!

4.2 std::this_thread::sleep_for

//============================================================================
// Name        : std::this_thread::sleep_for
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>       // std::cout, std::endl
#include <thread>         // std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds

int main()
{
	using namespace std::chrono_literals;

	std::cout << "Hello waiter\n" << std::flush;

    // C++14
	auto start = std::chrono::high_resolution_clock::now();
	std::this_thread::sleep_for(2s);
	auto end = std::chrono::high_resolution_clock::now();

	std::chrono::duration<double, std::milli> elapsed = end - start;
	std::cout << "Waited " << elapsed.count() << " ms\n";

	return 0;
}

10:22:39 **** Incremental Build of configuration Debug for project hello_world ****
make all 
Building file: ../src/hello_world.cpp
Invoking: GCC C++ Compiler
g++ -std=c++1y -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/hello_world.d" -MT"src/hello_world.o" -o "src/hello_world.o" "../src/hello_world.cpp"
Finished building: ../src/hello_world.cpp
 
Building target: hello_world
Invoking: GCC C++ Linker
g++  -o "hello_world"  ./src/hello_world.o   -lpthread
Finished building target: hello_world
 

10:22:40 Build Finished (took 895ms)
Hello waiter
Waited 2000.06 ms

7. 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).
如果 rel_time 的类型从不抛出异常 (如头文件 <chrono> 中的 duration` 实例化),则此函数从不抛出异常 (无抛出保证)。

assignment [ə'saɪnmənt]:n. 任务,布置,赋值

References

http://www.cplusplus.com/reference/thread/this_thread/sleep_for/
https://en.cppreference.com/w/cpp/thread/sleep_for

发布了509 篇原创文章 · 获赞 1824 · 访问量 110万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104587523