std::this_thread::sleep_for解析
是 C++ 标准库中提供的一个函数,属于头文件。其作用是让当前线程暂停执行(也就是 "睡眠")一段指定的时间。namespace std {namespace this_thread {template< class Rep, class Period >void sleep_for( const std::chrono::duration<Rep, Period>&
std::this_thread::sleep_for 是 C++ 标准库中提供的一个函数,属于 <thread> 头文件。其作用是让当前线程暂停执行(也就是 "睡眠")一段指定的时间。
函数原型
namespace std {
namespace this_thread {
template< class Rep, class Period >
void sleep_for( const std::chrono::duration<Rep, Period>& rel_time );
}
}
主要参数
-
rel_time:表示线程需要暂停的时间长度,是以std::chrono::duration为单位的时间对象。
用途
线程调用 sleep_for 时,会暂停当前线程一段时间,直到指定的时间结束后线程恢复运行。这是一个简单的延时机制,常用于以下场景:
-
控制程序节奏:
-
在需要周期性地执行某段代码时,通过
sleep_for实现固定时间间隔。
-
-
给其他线程让出资源:
-
让当前线程暂时休眠,允许其他线程获得 CPU 时间片。
-
-
等待硬件响应或任务准备完成:
-
在需要等待外部硬件或设备完成某些工作时,暂停当前线程的运行。
-
典型使用方式
以下是使用 std::this_thread::sleep_for 的一些常见场景和示例:
1. 基本例子
#include <iostream>
#include <thread>
#include <chrono>
int main() {
std::cout << "Start waiting..." << std::endl;
// 暂停当前线程 2 秒钟
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "2 seconds have passed!" << std::endl;
return 0;
}
输出结果:
Start waiting... 2 seconds have passed!
-
代码暂停 2 秒后继续执行。
-
使用
std::chrono::seconds函数来指定延时时间。
2. 更高精度的暂停
可以根据业务需求选择不同的时间精度(秒、毫秒、微秒等)。
#include <iostream>
#include <thread>
#include <chrono>
int main() {
std::cout << "Start" << std::endl;
// 暂停当前线程 1.5 秒钟 (= 1500 毫秒)
std::this_thread::sleep_for(std::chrono::milliseconds(1500));
std::cout << "End after 1.5 seconds!" << std::endl;
return 0;
}
输出:
Start End after 1.5 seconds!
3. 精细时间间隔控制(伺服控制中典型场景)
在循环中加入 sleep_for,可以按照固定时间间隔执行任务。
#include <iostream>
#include <thread>
#include <chrono>
int main() {
for (int i = 0; i < 5; ++i) {
std::cout << "Iteration " << i+1 << std::endl;
// 每次循环暂停 1 秒
std::this_thread::sleep_for(std::chrono::seconds(1));
}
std::cout << "Done!" << std::endl;
return 0;
}
输出:
Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5 Done!
解释:
-
每次循环后暂停 1 秒,形成了一个固定的时间间隔,通常用于周期性任务调度。
4. 更平滑的实时控制(比如在伺服控制中使用)
在伺服电机的同步控制中,需要精确控制插补周期(例如 4ms)。通过 std::this_thread::sleep_for(std::chrono::milliseconds(4)),可以模拟周期性的点位控制。
#include <iostream>
#include <thread>
#include <chrono>
int main() {
int position = 0;
int step = 100; // 每 4 毫秒增加一步
for (int i = 0; i < 10; ++i) {
position += step;
std::cout << "Sending position: " << position << std::endl;
// 停顿 4 毫秒
std::this_thread::sleep_for(std::chrono::milliseconds(4));
}
std::cout << "Control finished!" << std::endl;
return 0;
}
与其他线程睡眠函数的对比
| 函数 | 特点 | 注意事项 |
|---|---|---|
std::this_thread::sleep_for |
线程暂停一段相对时间 | 精度依赖于系统时钟(可能会有一定误差),不适合高精度实时控制任务 |
std::this_thread::sleep_until |
暂停直到某个系统时间点 | 对绝对时间的任务更有用 |
std::this_thread::yield |
主动释放当前线程的 CPU 时间片 | 不会暂停线程,仅让当前线程进入可执行队列 |
注意事项
-
精确度问题:
-
std::this_thread::sleep_for的精度由操作系统和硬件支持决定。精度通常足够用在毫秒级别的任务上,但在纳秒级和高实时性任务(如伺服控制)中可能表现不足。 -
如果需要更高的时间精度,可以结合硬件时钟或专用计时器(如
std::chrono::high_resolution_clock)。
-
-
阻塞机制:
-
sleep_for会完全阻塞线程,暂停期间线程无法处理其他任务。如果需要非阻塞的等待,可以考虑其他异步方法(例如事件触发或回调机制)。
-
-
与操作系统相关:
-
不同操作系统对睡眠时间的最小分辨率有不同约束,例如一些操作系统休眠时间最小单位为 1 毫秒。
-
总结
-
功能:
std::this_thread::sleep_for是一种暂停线程的方法,通常用于控制执行间隔、等待任务完成或模拟实时系统中的周期任务。 -
适用场景:有周期性任务、低实时性要求的时间间隔控制时非常适用。
-
局限性:在高精度、实时性要求较高(如 1 微秒以内控制)的场景下可能无法满足需求。
更多推荐



所有评论(0)