不可预测的 C++ sleep/wait 行为
unpredictable C++ sleep/wait behavior
我一定是在做一些愚蠢的事情,因为我从这个简单的睡眠代码中得到了最奇怪的行为。最初我使用 std::this_thread::sleep_for 并得到了相同的结果,但假设它一定是一些线程奇怪。但是,我在等待下面的代码时遇到了同样看似无序的等待。使用 clang++ 或 g++ 结果相同。我在 Debian 上并在命令行编译。
预期行为:
将在 3... [等待一秒钟] 2... [等待一秒钟] 1... [等待一秒钟;程序退出]
实际行为:
[3秒长等待] 3...2...1...关机中[程序退出]
#include<chrono>
#include<iostream>
void Sleep(int i) {
auto start = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::seconds>(now-start).count() < i)
now = std::chrono::high_resolution_clock::now();
}
void ShutdownCountdown(int i) {
if (i <= 0) return;
std::cout << "Shutting down in ";
for (; i != 0; --i) {
std::cout << i << "... ";
Sleep(1);
}
std::cout << std::endl << std::endl;
}
int main (int argc, char *argv[]) {
ShutdownCountdown(3);
return 0;
}
#include<chrono>
#include<iostream>
void Sleep(int i) {
auto start = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::seconds>(now-start).count() < i)
now = std::chrono::high_resolution_clock::now();
}
void ShutdownCountdown(int i) {
if (i <= 0) return;
std::cout << "Shutting down in "<<std::flush;
for (; i != 0; --i) {
std::cout << i << "... "<<std::flush;
Sleep(1);
}
std::cout << std::endl << std::endl;
}
int main (int argc, char *argv[]) {
ShutdownCountdown(3);
return 0;
}
通常 iostream
s 在遇到换行符之前不会刷新。由于您不输出 EOL 字符,因此需要显式刷新以打印输出:
std::cout << i << "... " << std::flush;
无关,但还要注意 CPU 当您 运行 您的程序时变得有点 hot。为了节省能源,考虑将繁忙的循环改回真实的 sleep:
for (; i != 0; --i) {
std::cout << i << "... " << std::flush;
std::this_thread::sleep_for(1s);
}
漂亮的“1s
”语法可以在程序开头使用 using namespace std::chrono_literals;
。
我一定是在做一些愚蠢的事情,因为我从这个简单的睡眠代码中得到了最奇怪的行为。最初我使用 std::this_thread::sleep_for 并得到了相同的结果,但假设它一定是一些线程奇怪。但是,我在等待下面的代码时遇到了同样看似无序的等待。使用 clang++ 或 g++ 结果相同。我在 Debian 上并在命令行编译。
预期行为: 将在 3... [等待一秒钟] 2... [等待一秒钟] 1... [等待一秒钟;程序退出]
实际行为: [3秒长等待] 3...2...1...关机中[程序退出]
#include<chrono>
#include<iostream>
void Sleep(int i) {
auto start = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::seconds>(now-start).count() < i)
now = std::chrono::high_resolution_clock::now();
}
void ShutdownCountdown(int i) {
if (i <= 0) return;
std::cout << "Shutting down in ";
for (; i != 0; --i) {
std::cout << i << "... ";
Sleep(1);
}
std::cout << std::endl << std::endl;
}
int main (int argc, char *argv[]) {
ShutdownCountdown(3);
return 0;
}
#include<chrono>
#include<iostream>
void Sleep(int i) {
auto start = std::chrono::high_resolution_clock::now();
auto now = std::chrono::high_resolution_clock::now();
while (std::chrono::duration_cast<std::chrono::seconds>(now-start).count() < i)
now = std::chrono::high_resolution_clock::now();
}
void ShutdownCountdown(int i) {
if (i <= 0) return;
std::cout << "Shutting down in "<<std::flush;
for (; i != 0; --i) {
std::cout << i << "... "<<std::flush;
Sleep(1);
}
std::cout << std::endl << std::endl;
}
int main (int argc, char *argv[]) {
ShutdownCountdown(3);
return 0;
}
通常 iostream
s 在遇到换行符之前不会刷新。由于您不输出 EOL 字符,因此需要显式刷新以打印输出:
std::cout << i << "... " << std::flush;
无关,但还要注意 CPU 当您 运行 您的程序时变得有点 hot。为了节省能源,考虑将繁忙的循环改回真实的 sleep:
for (; i != 0; --i) {
std::cout << i << "... " << std::flush;
std::this_thread::sleep_for(1s);
}
漂亮的“1s
”语法可以在程序开头使用 using namespace std::chrono_literals;
。