如何在 C++ 中實現毫秒級的睡眠
本文介紹了在 C++ 中睡眠毫秒的方法。
使用 std::this_thread::sleep_for
方法在 C++ 中睡眠
這個方法是 <thread>
庫中 sleep
函式的純 C++ 版本,它是 Windows 和 Unix 平臺的可移植版本。為了更好地演示示例,我們暫停程序 3000 毫秒。
#include <iostream>
#include <chrono>
#include <thread>
using std::cout;
using std::cin;
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
我們可以使用 std::chono_literals
名稱空間重寫上面的程式碼,使其更有說服力。
#include <iostream>
#include <thread>
using std::cout;
using std::cin;
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 <iostream>
#include <unistd.h>
using std::cout;
using std::cin;
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 <iostream>
#include <unistd.h>
#define SLEEP_MS(milsec) usleep(milsec * 1000)
using std::cout;
using std::cin;
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 <iostream>
#include <ctime>
using std::cout;
using std::cin;
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;
}
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn