std::this_thread::sleep_for

std::this_thread::sleep_for

Defined in header – 定义于头文件

  1. std::this_thread::sleep_for

template
void sleep_for (const chrono::duration& 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 为止。其他线程继续执行。

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

要睡眠的时长。

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

1
  1. Return value

none

  1. 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 // std::cout, std::endl

include // std::this_thread::sleep_for

include // 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;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Output (after 10 seconds):

countdown:
10
9
8
7
6
5
4
3
2
1
Lift off!

1
2
3
4
5
6
7
8
9
10
11
12
13

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 // std::cout, std::endl

include // std::this_thread::sleep_for

include // 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;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14

Hello waiter
Waited 2000.06 ms

1
2
3
  1. Exception safety – 异常安全性

If the type of rel_time never throws exceptions (like the instantiations of duration in header ), this function never throws exceptions (no-throw guarantee).
如果 rel_time 的类型从不抛出异常 (如头文件 中的 duration` 实例化),则此函数从不抛出异常 (无抛出保证)。

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

1

References

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