休眠几毫秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4184468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:51:46  来源:igfitidea点击:

Sleep for milliseconds

c++linuxsleep

提问by Prasanth Madhavan

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 millisecondsin C++?

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

采纳答案by Niet the Dark Absol

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

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

#include <unistd.h>

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

回答by MOnsDaR

To stay portable you could use Boost::Threadfor sleeping:

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

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

这个答案是重复的,之前已经在这个问题中发布过。也许你也可以在那里找到一些有用的答案。

回答by Johan Kotlinski

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

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

回答by CB Bailey

Depending on your platform you may have usleepor nanosleepavailable. usleepis deprecated and has been deleted from the most recent POSIX standard; nanosleepis preferred.

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

回答by INS

In Unix you can use usleep.

在 Unix 中,您可以使用usleep

In Windows there is Sleep.

在 Windows 中有Sleep

回答by Madhava Gaikwad

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

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

回答by HighCommander4

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

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

#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.

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

回答by foobar

#include <windows.h>

Syntax:

句法:

Sleep (  __in DWORD dwMilliseconds   );

Usage:

用法:

Sleep (1000); //Sleeps for 1000 ms or 1 sec

回答by Bart Grzybicki

Why don't use time.h library? Runs on Windows and POSIX systems:

为什么不使用 time.h 库?在 Windows 和 POSIX 系统上运行:

#include <iostream>
#include <time.h>

using namespace std;

void sleepcp(int milliseconds);

void sleepcp(int milliseconds) // Cross-platform sleep function
{
    clock_t time_end;
    time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
    while (clock() < time_end)
    {
    }
}
int main()
{
    cout << "Hi! At the count to 3, I'll die! :)" << endl;
    sleepcp(3000);
    cout << "urrrrggghhhh!" << endl;
}

Corrected code - now CPU stays in IDLE state [2014.05.24]:

更正的代码 - 现在 CPU 处于空闲状态 [2014.05.24]:

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

回答by Phi

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

在 C++ 中休眠程序的Sleep(int)方法是方法。它的头文件是#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