迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

如何在 C++ 中实现毫秒级的睡眠

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文介绍了在 C++ 中睡眠毫秒的方法。


使用 std::this_thread::sleep_for 方法在 C++ 中睡眠

这个方法是 <thread> 库中 sleep 函数的纯 C++ 版本,它是 Windows 和 Unix 平台的可移植版本。为了更好地演示示例,我们暂停进程 3000 毫秒。

#include <chrono>
#include <iostream>
#include <thread>

using std::cin;
using std::cout;
using std::endl;
using std::this_thread::sleep_for;

constexpr int TIME_TO_SLEEP = 3000;

int main() {
  cout << "Started loop.." << endl;
  for (int i = 0; i < 10; ++i) {
    cout << "Iteration - " << i << endl;

    if (i == 4) {
      cout << "Sleeping ...." << endl;
      sleep_for(std::chrono::milliseconds(TIME_TO_SLEEP));
    }
  }
  return 0;
}

输出:

Started loop...
Iteration - 0
Iteration - 1
Iteration - 2
Iteration - 3
Iteration - 4
Sleeping ....
Iteration - 5
Iteration - 6
Iteration - 7
Iteration - 8
Iteration - 9

如何在 Cpp 中睡觉

我们可以使用 std::chono_literals 命名空间重写上面的代码,使其更有说服力。

#include <iostream>
#include <thread>

using std::cin;
using std::cout;
using std::endl;
using std::this_thread::sleep_for;
using namespace std::chrono_literals;

int main() {
  cout << "Started loop.." << endl;
  for (int i = 0; i < 10; ++i) {
    cout << "Iteration - " << i << endl;

    if (i == 4) {
      cout << "Sleeping ...." << endl;
      sleep_for(3000ms);
    }
  }
  return 0;
}

在 C++ 中使用 usleep 函数进行睡眠

usleep 是在 <unistd.h> 头中定义的一个 POSIX 专用函数,它接受微秒数作为参数,它应该是无符号整数类型,能够容纳 [0,1000000]范围内的整数。

#include <unistd.h>

#include <iostream>

using std::cin;
using std::cout;
using std::endl;

constexpr int TIME_TO_SLEEP = 3000;

int main() {
  cout << "Started loop.." << endl;
  for (int i = 0; i < 10; ++i) {
    cout << "Iteration - " << i << endl;

    if (i == 4) {
      cout << "Sleeping ...." << endl;
      usleep(TIME_TO_SLEEP * 1000);
      ;
    }
  }
  return 0;
}

另外,我们也可以使用 usleep 函数定义一个自定义的宏,并做出一个更可重用的代码片段。

#include <unistd.h>

#include <iostream>

#define SLEEP_MS(milsec) usleep(milsec * 1000)

using std::cin;
using std::cout;
using std::endl;

constexpr int TIME_TO_SLEEP = 3000;

int main() {
  cout << "Started loop.." << endl;
  for (int i = 0; i < 10; ++i) {
    cout << "Iteration - " << i << endl;

    if (i == 4) {
      cout << "Sleeping ...." << endl;
      SLEEP_MS(TIME_TO_SLEEP);
    }
  }
  return 0;
}

使用 nanosleep 函数在 C++ 中休眠

nanosleep 函数是另一个 POSIX 特定版本,它提供了更好的处理中断的功能,并且对睡眠间隔有更精细的分辨率。也就是说,程序员创建一个 timespec 结构,分别指定秒数和纳秒数。nanosleep 也会取第二个 timespec 结构参数来返回剩余的时间间隔,以防程序被信号打断。注意,中断处理由程序员负责。

#include <ctime>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

constexpr int SECS_TO_SLEEP = 3;
constexpr int NSEC_TO_SLEEP = 3;

int main() {
  struct timespec request {
    SECS_TO_SLEEP, NSEC_TO_SLEEP
  }, remaining{SECS_TO_SLEEP, NSEC_TO_SLEEP};

  cout << "Started loop.." << endl;
  for (int i = 0; i < 10; ++i) {
    cout << "Iteration - " << i << endl;
    if (i == 4) {
      cout << "Sleeping ...." << endl;
      nanosleep(&request, &remaining);
    }
  }
  return 0;
}

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便