睡眠毫秒

时间:2010-11-15 12:49:52

标签: c++ linux sleep

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

18 个答案:

答案 0 :(得分:1057)

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

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

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

答案 1 :(得分:408)

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

#include <unistd.h>

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

答案 2 :(得分:78)

为了保持便携性,您可以使用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 question发布。也许你也可以找到一些有用的答案。

答案 3 :(得分:30)

根据您的平台,您可以usleepnanosleepusleep已弃用,已从最新的POSIX标准中删除; nanosleep是首选。

答案 4 :(得分:30)

在Unix中,您可以使用usleep

在Windows中有Sleep

答案 5 :(得分:18)

为什么不使用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;
}

更正后的代码 - 现在CPU处于IDLE状态[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;
}

答案 6 :(得分:17)

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

答案 7 :(得分:11)

#include <windows.h>

语法:

Sleep (  __in DWORD dwMilliseconds   );

用法:

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

答案 8 :(得分:7)

如果使用MS Visual C ++ 10.0,则可以使用标准库工具执行此操作:

Concurrency::wait(milliseconds);

你需要:

#include <concrt.h>

答案 9 :(得分:5)

#include <chrono>
#include <thread>

std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // sleep for 1 second

请记住要导入两个标头。

答案 10 :(得分:4)

使用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

答案 11 :(得分:4)

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

#include <chrono>
#include <thread>

using namespace std::chrono;

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

答案 12 :(得分:4)

在具有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.
    }
}

然而,现在有更好的替代方案。

答案 13 :(得分:3)

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

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

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

答案 14 :(得分:3)

选择呼叫是一种具有更高精度的方式(睡眠时间可以以纳秒为单位指定)。

答案 15 :(得分:0)

作为POSIX系统的Win32替代品:

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

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

答案 16 :(得分:0)

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

#ifndef MACROS_H
#define MACROS_H

#include <unistd.h>

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

#endif // MACROS_H

答案 17 :(得分:-3)

在gcc中用于C///。

#include

然后使用Sleep(); ///使用大写字母S的sleep()。不使用s的sleep()。

// Sleep(1000)是1秒///也许。

clang支持sleep(),sleep(1)的延迟时间为1秒。

相关问题