休眠几毫秒

发布于 2024-10-02 16:46:45 字数 86 浏览 1 评论 0原文

我知道 POSIX sleep(x) 函数使程序休眠 x 秒。 C++ 中是否有一个函数可以让程序休眠 x 毫秒

I know the POSIX sleep(x) function makes the program sleep for x seconds. Is there a function to make the program sleep for x milliseconds in C++?

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

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

发布评论

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

评论(19

澜川若宁 2024-10-09 16:46:45

在 C++11 中,您可以使用标准库工具来完成此操作:

#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));

清晰易读,不再需要猜测 sleep() 函数采用的单位。

In C++11, you can do this with standard library facilities:

#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(x));

Clear and readable, no more need to guess at what units the sleep() function takes.

栖竹 2024-10-09 16:46:45

请注意,没有针对毫秒的标准 C API,因此(在 Unix 上)您将不得不使用 usleep,它接受微秒:

#include <unistd.h>

unsigned int microseconds;
...
usleep(microseconds);

Note that there is no standard C API for milliseconds, so (on Unix) you will have to settle for usleep, which accepts microseconds:

#include <unistd.h>

unsigned int microseconds;
...
usleep(microseconds);
闻呓 2024-10-09 16:46:45

为了保持便携性,您可以使用 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;
}

这个答案是重复的,并已发布在此问题< /a> 之前。也许您也可以在那里找到一些有用的答案。

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;
}

This answer is a duplicate and has been posted in this question before. Perhaps you could find some usable answers there too.

秋风の叶未落 2024-10-09 16:46:45

在 Unix 中,您可以使用 usleep

在 Windows 中,有 睡眠< /a>.

In Unix you can use usleep.

In Windows there is Sleep.

最近可好 2024-10-09 16:46:45

从 C++14 开始,使用 std 及其数字文字:

#include <chrono>
#include <thread>

using namespace std::chrono_literals;

std::this_thread::sleep_for(123ms);

From C++14 using std and also its numeric literals:

#include <chrono>
#include <thread>

using namespace std::chrono_literals;

std::this_thread::sleep_for(123ms);
恏ㄋ傷疤忘ㄋ疼 2024-10-09 16:46:45

根据您的平台,您可能有 usleepnanosleep 可用。 usleep 已弃用,并已从最新的 POSIX 标准中删除; nanosleep 是首选。

Depending on your platform you may have usleep or nanosleep available. usleep is deprecated and has been deleted from the most recent POSIX standard; nanosleep is preferred.

握住你手 2024-10-09 16:46:45

为什么不使用 time.h 库?在 Windows 和 POSIX 系统上运行(不要在生产中使用此代码!):

CPU 保持 IDLE 状态:

#include <iostream>
#ifdef _WIN32
    #include <windows.h>
#else
    #include <unistd.h>
#endif // _WIN32

using namespace std;

void sleepcp(int milliseconds);

void sleepcp(int milliseconds) // Cross-platform sleep function
{
    #ifdef _WIN32
        Sleep(milliseconds);
    #else
        usleep(milliseconds * 1000);
    #endif // _WIN32
}
int main()
{
    cout << "Hi! At the count to 3, I'll die! :)" << endl;
    sleepcp(3000);
    cout << "urrrrggghhhh!" << endl;
}

Why don't use time.h library? Runs on Windows and POSIX systems (don't use this code in production!):

CPU stays in IDLE state:

#include <iostream>
#ifdef _WIN32
    #include <windows.h>
#else
    #include <unistd.h>
#endif // _WIN32

using namespace std;

void sleepcp(int milliseconds);

void sleepcp(int milliseconds) // Cross-platform sleep function
{
    #ifdef _WIN32
        Sleep(milliseconds);
    #else
        usleep(milliseconds * 1000);
    #endif // _WIN32
}
int main()
{
    cout << "Hi! At the count to 3, I'll die! :)" << endl;
    sleepcp(3000);
    cout << "urrrrggghhhh!" << endl;
}
鹊巢 2024-10-09 16:46:45

nanosleep 是比 usleep 更好的选择 - 它对中断更具弹性。

nanosleep is a better choice than usleep - it is more resilient against interrupts.

冰雪之触 2024-10-09 16:46:45
#include <windows.h>

语法:

Sleep (  __in DWORD dwMilliseconds   );

用法:

Sleep (1000); //Sleeps for 1000 ms or 1 sec
#include <windows.h>

Syntax:

Sleep (  __in DWORD dwMilliseconds   );

Usage:

Sleep (1000); //Sleeps for 1000 ms or 1 sec
羅雙樹 2024-10-09 16:46:45

如果使用 MS Visual C++ 10.0,您可以使用标准库工具来完成此操作:

Concurrency::wait(milliseconds);

您将需要:

#include <concrt.h>

If using MS Visual C++ 10.0, you can do this with standard library facilities:

Concurrency::wait(milliseconds);

you will need:

#include <concrt.h>
木落 2024-10-09 16:46:45

在具有 select 功能的平台(POSIX、Linux 和 Windows)上,您可以这样做:

void sleep(unsigned long msec) {
    timeval delay = {msec / 1000, msec % 1000 * 1000};
    int rc = ::select(0, NULL, NULL, NULL, &delay);
    if(-1 == rc) {
        // Handle signals by continuing to sleep or return immediately.
    }
}

但是,现在有更好的替代方案。

On platforms with the select function (POSIX, Linux, and Windows) you could do:

void sleep(unsigned long msec) {
    timeval delay = {msec / 1000, msec % 1000 * 1000};
    int rc = ::select(0, NULL, NULL, NULL, &delay);
    if(-1 == rc) {
        // Handle signals by continuing to sleep or return immediately.
    }
}

However, there are better alternatives available nowadays.

束缚m 2024-10-09 16:46:45

Select 调用是一种更精确的方法(睡眠时间可以以纳秒为单位指定)。

Select call is a way of having more precision (sleep time can be specified in nanoseconds).

星星的轨迹 2024-10-09 16:46:45

在 C++ 中让程序休眠的方法是 Sleep(int) 方法。它的头文件是#include "windows.h."

例如:

#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;

int main()
{
    int x = 6000;
    Sleep(x);
    cout << "6 seconds have passed" << endl;
    return 0;
}

它的睡眠时间以毫秒为单位,没有限制。

Second = 1000 milliseconds
Minute = 60000 milliseconds
Hour = 3600000 milliseconds

The way to sleep your program in C++ is the Sleep(int) method. The header file for it is #include "windows.h."

For example:

#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;

int main()
{
    int x = 6000;
    Sleep(x);
    cout << "6 seconds have passed" << endl;
    return 0;
}

The time it sleeps is measured in milliseconds and has no limit.

Second = 1000 milliseconds
Minute = 60000 milliseconds
Hour = 3600000 milliseconds
寄人书 2024-10-09 16:46:45

C++14:休眠 10 秒:

#include <thread>
#include <chrono>

using namespace std::chrono_literals;
std::this_thread::sleep_for(10s);

10 毫秒使用 10ms

C++14: sleep for 10 seconds:

#include <thread>
#include <chrono>

using namespace std::chrono_literals;
std::this_thread::sleep_for(10s);

for 10 milliseconds use 10ms

山色无中 2024-10-09 16:46:45

使用Boost异步输入/输出线程,休眠x毫秒;

#include <boost/thread.hpp>
#include <boost/asio.hpp>

boost::thread::sleep(boost::get_system_time() + boost::posix_time::millisec(1000));

Use Boost asynchronous input/output threads, sleep for x milliseconds;

#include <boost/thread.hpp>
#include <boost/asio.hpp>

boost::thread::sleep(boost::get_system_time() + boost::posix_time::millisec(1000));
花落人断肠 2024-10-09 16:46:45

来自一个答案的优雅解决方案,经过一点修改。如果没有更好的功能可用,可以轻松添加 select() 用法。只需制作使用 select() 等的函数即可。

代码:


#include <iostream>

/*
 Prepare defines for millisecond sleep function that is cross-platform
*/
#ifdef _WIN32
#  include <Windows.h>
#  define sleep_function_name           Sleep
#  define sleep_time_multiplier_for_ms      1
#else
#  include <unistd.h>
#  define sleep_function_name           usleep
#  define sleep_time_multiplier_for_ms      1000
#endif

/* Cross platform millisecond sleep */
void cross_platform_sleep_ms(unsigned long int time_to_sleep_in_ms)
{
   sleep_function_name ( sleep_time_multiplier_for_ms * time_to_sleep_in_ms );
}

Elegant solution from the one answer, bit modified.. One can easilly add select() usage if there's no better functionality available. Just make function that uses select() etc. ..

Code:


#include <iostream>

/*
 Prepare defines for millisecond sleep function that is cross-platform
*/
#ifdef _WIN32
#  include <Windows.h>
#  define sleep_function_name           Sleep
#  define sleep_time_multiplier_for_ms      1
#else
#  include <unistd.h>
#  define sleep_function_name           usleep
#  define sleep_time_multiplier_for_ms      1000
#endif

/* Cross platform millisecond sleep */
void cross_platform_sleep_ms(unsigned long int time_to_sleep_in_ms)
{
   sleep_function_name ( sleep_time_multiplier_for_ms * time_to_sleep_in_ms );
}

双手揣兜 2024-10-09 16:46:45

作为 POSIX 系统的 Win32 替代品:

void Sleep(unsigned int milliseconds) {
    usleep(milliseconds * 1000);
}

while (1) {
    printf(".");
    Sleep((unsigned int)(1000.0f/20.0f)); // 20 fps
}

As a Win32 replacement for POSIX systems:

void Sleep(unsigned int milliseconds) {
    usleep(milliseconds * 1000);
}

while (1) {
    printf(".");
    Sleep((unsigned int)(1000.0f/20.0f)); // 20 fps
}
素罗衫 2024-10-09 16:46:45

这个问题很老了,但我设法找到了一种简单的方法来将其添加到我的应用程序中。您可以创建一个 C/C++ 宏,如下所示使用它:

#ifndef MACROS_H
#define MACROS_H

#include <unistd.h>

#define msleep(X) usleep(X * 1000)

#endif // MACROS_H

The question is old, but I managed to figure out a simple way to have this in my app. You can create a C/C++ macro as shown below use it:

#ifndef MACROS_H
#define MACROS_H

#include <unistd.h>

#define msleep(X) usleep(X * 1000)

#endif // MACROS_H
子栖 2024-10-09 16:46:45

我用这个:

#include <thread>
#define sleepms(val) std::this_thread::sleep_for(val##ms)

示例:

sleepms(200);

I use this:

#include <thread>
#define sleepms(val) std::this_thread::sleep_for(val##ms)

example:

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