需要更好的等待解决方案

发布于 2024-10-01 05:52:12 字数 456 浏览 0 评论 0原文

最近我一直在用 C++ 编写一个程序,它可以 ping 三个不同的网站,然后根据通过或失败的情况,它会等待 5 分钟或 30 秒,然后再重试。

目前我一直在使用ctime库和以下函数来处理我的等待。然而,根据我的 CPU 计量表,这是一个不可接受的解决方案。

void wait (int seconds)
{
   clock_t endwait;
   endwait = clock () + seconds * CLOCKS_PER_SEC;
   while (clock () < endwait) {}
}

这个解决方案不可接受的原因是,根据我的 CPU 计量表,程序在等待时以 48% 到 50% 的 CPU 运行。我有一个 Athlon 64 x2 1.2 GHz 处理器。我的 130 行程序不可能达到接近 50% 的程度。

如何更好地编写等待函数,使其仅使用最少的资源?

Recently I have been writing a program in C++ that pings three different websites and then depending on pass or fail it will wait 5 minutes or 30 seconds before it tries again.

Currently I have been using the ctime library and the following function to process my waiting. However, according to my CPU meter this is an unacceptable solution.

void wait (int seconds)
{
   clock_t endwait;
   endwait = clock () + seconds * CLOCKS_PER_SEC;
   while (clock () < endwait) {}
}

The reason why this solution is unacceptable is because according to my CPU meter the program runs at 48% to 50% of my CPU when waiting. I have a Athlon 64 x2 1.2 GHz processor. There is no way my modest 130 line program should even get near 50%.

How can I write my wait function better so that it is only using minimal resources?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

荒岛晴空 2024-10-08 05:52:12

2023 年更新:现在您可能不再使用 Boot 来完成此任务,因为 C++ 标准库在 C++11 中添加了对休眠的本机支持。请检查下面更合适的答案。

为了保持可移植性,您可以使用 Boost::Thread 用于睡眠:

#include <boost/thread/thread.hpp>

int main()
{
    //waits 2 seconds
    boost::this_thread::sleep( boost::posix_time::seconds(1) );
    boost::this_thread::sleep( boost::posix_time::milliseconds(1000) );

    return 0;
}

Update in 2023: Nowadays you'd probably not use Boot anymore for this task since the C++ Standard library added native support for sleeping back in C++11. Please check the more appropriate answers below.

To stay portable you could use Boost::Thread for sleeping:

#include <boost/thread/thread.hpp>

int main()
{
    //waits 2 seconds
    boost::this_thread::sleep( boost::posix_time::seconds(1) );
    boost::this_thread::sleep( boost::posix_time::milliseconds(1000) );

    return 0;
}
深空失忆 2024-10-08 05:52:12

对于 C++11 标准,可以使用以下方法:

std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::seconds(100));

或者可以使用 sleep_until

With the C++11 standard the following approach can be used:

std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::seconds(100));

Alternatively sleep_until could be used.

天冷不及心凉 2024-10-08 05:52:12

只是为了解释发生了什么:当你调用clock()时,你的程序再次检索时间:你要求它尽可能快地执行此操作,直到到达结束时间......这使得运行程序的CPU核心“旋转” “尽可能快地通过你的循环,每秒读取时间数百万次,希望它能滚动到结束时间。相反,您需要告诉操作系统您希望在一段时间后被唤醒...然后他们可以挂起您的程序并让其他程序运行(或系统空闲)...这就是其他中提到的各种睡眠功能答案是为了。

Just to explain what's happening: when you call clock() your program retrieves the time again: you're asking it to do that as fast as it can until it reaches the endtime... that leaves the CPU core running the program "spinning" as fast as it can through your loop, reading the time millions of times a second in the hope it'll have rolled over to the endtime. You need to instead tell the operating system that you want to be woken up after an interval... then they can suspend your program and let other programs run (or the system idle)... that's what the various sleep functions mentioned in other answers are for.

清旖 2024-10-08 05:52:12

使用 sleep 而不是空的 while 循环。

Use sleep rather than an empty while loop.

滿滿的愛 2024-10-08 05:52:12

windows.h 中有 Sleep,*nix 上 unistd.h 中有 sleep。

有一个更优雅的解决方案@ http://www.faqs .org/faqs/unix-faq/faq/part4/section-6.html

There's Sleep in windows.h, on *nix there's sleep in unistd.h.

There's a more elegant solution @ http://www.faqs.org/faqs/unix-faq/faq/part4/section-6.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文