简体   繁体   English

C ++函数在windows中完美运行但不是Linux吗?

[英]C++ function runs in windows perfectly but not linux?

I am trying to write a simple c++ function sleep(int millisecond) that will sleep the program for user-specific millisecond. 我正在尝试编写一个简单的c ++函数sleep(int millisecond) ,它将使程序睡眠用户特定的毫秒。

Here is my code: 这是我的代码:

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

using namespace std;

void sleep(unsigned int mseconds) {
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main() {
    cout << "Hello World !" << endl;
    sleep(3000);
    cout << "Hello World 2" << endl;
}

The sleep() function works perfectly when I run this code on windows but doesn't work on Linux. 当我在Windows上运行此代码但在Linux上无法运行时, sleep()函数可以正常工作。 Can anyone figure it out what's wrong with my code? 任何人都能弄明白我的代码有什么问题吗?

I don't know why everyone is dancing around your question instead of answering it. 我不知道为什么每个人都在围绕你的问题跳舞而不是回答它。

You are attempting to implement your own sleep-like function, and your implementation, while it does busy wait instead of sleeping in the kernelspace (meaning that processor will be "actively" running code to sleep your program, instead of telling the machine your program is sleeping and it should run other code), is just fine. 您正在尝试实现自己的类似睡眠的功能和实现,同时它忙于等待而不是在内核空间中休眠(这意味着处理器将“主动”运行代码来睡眠您的程序,而不是告诉机器您的程序正在睡觉,它应该运行其他代码),就好了。

The problem is that clock() is not required to return milliseconds. 问题是clock()不需要返回毫秒。 clock() will return system time/process time elapsed in ticks from epoch. clock()将返回从纪元开始经过的系统时间/处理时间。 What unit that time will take depends on the implementation. 该时间占用的单位取决于实施情况。

For instance, on my machine, this is what the man page says: 例如,在我的机器上,这是手册页所说的内容:

DESCRIPTION 描述

The clock() function determines the amount of processor time used since the invocation of the calling process, measured in CLOCKS_PER_SECs of a second. clock()函数确定自调用调用进程以来使用的处理器时间量,以一秒钟的CLOCKS_PER_SEC为单位进行测量。

RETURN VALUES 返回值

The clock() function returns the amount of time used unless an error occurs, in which case the return value is -1. clock()函数返回使用的时间量,除非发生错误,在这种情况下返回值为-1。

SEE ALSO 也可以看看

getrusage(2), clocks(7) getrusage(2),时钟(7)

STANDARDS 标准

The clock() function conforms to ISO/IEC 9899:1990 (``ISO C90'') and Version 3 of the Single UNIX Specification (``SUSv3'') which requires CLOCKS_PER_SEC to be defined as one million . clock()函数符合ISO / IEC 9899:1990(``ISO C90'')和单UNIX规范的版本3(``SUSv3'') ,它要求将CLOCKS_PER_SEC定义为一百万

As you can see from the bolded part, a tick is one-one-millionth of a second, aka a microsecond (not a millisecond). 从粗体部分可以看出,刻度是百万分之一秒,即微秒(不是毫秒)。 To "sleep" for 3 seconds, you'll need to call your sleep(3000000) and not sleep(3000) . 要“睡眠”3秒钟,你需要打电话给你的sleep(3000000)而不是sleep(3000)

With C++11 you can use sleep_for . 使用C ++ 11,您可以使用sleep_for

#include <chrono>
#include <thread>

void sleep(unsigned int mseconds) {
    std::chrono::milliseconds dura( mseconds);
    std::this_thread::sleep_for( dura );
}

You can use build-in sleep() function which takes pospond time as seconds not in milliseconds and have to include unistd.h standard library as build-in sleep() function is defined under this library. 你可以使用内置的sleep()函数,该函数以秒为单位,而不是毫秒,并且必须包含unistd.h标准库,因为内置的sleep()函数是在这个库下定义的。

Try it: 试试吧:

#include <iostream>
#include <unistd.h>

using namespace std;

int main() {
    cout << "Hello World !" << endl;
    sleep(3);   //wait for 3 seconds
    cout << "Hello World 2" << endl;
}

:P :P

There is no standard C API for milliseconds on Linux so you will have to use usleep . Linux上没有标准的C API(毫秒),所以你必须使用usleep POSIX sleep takes seconds. POSIX sleep需要几秒钟。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++程序可以在Linux上完美运行,但不能在Windows上运行 - C++ program run perfectly on Linux but can't run on Windows 在Linux上运行的C ++程序中计时功能 - Timing a function in a C++ program that runs on Linux 将在 windows 上运行的 Delphi 中的汇编程序 function 转换为 delphi/c++ 以在 ZEDC8310A5A3D57473E 上执行 - converting assembler function in Delphi which runs on windows to delphi/c++ to execute on Linux 在Windows上运行并生成Linux代码的C ++编译器 - C++ compiler that runs on windows and generates Linux code 如何在 Z6CE809EACF900BA125B40FABD90FABD90392E 中运行在 Z6CE809EACF900BA125B40FABD92E 中的 Z6CE809EACF900BA125Bshell 中编写一个 cp function (在 linux shell 中) - how to write a cp function (in linux shell ) in c++ that runs in background? C ++在Xcode控制台中未显示cout,但在Terminal中可完美运行 - C++ not showing cout in Xcode console but runs perfectly in Terminal 进行文件操作时,C ++程序在Windows和Linux上的运行速度要慢得多? - C++ program runs much slower on Windows vs Linux when doing file operations? C ++:std :: async和std :: mutex在Linux上导致死锁,但在Windows上运行? - C++: std::async and std::mutex leads to deadlock on Linux but runs on Windows? Windows Linux c ++编译问题:“调用没有匹配功能” - Windows Linux c++ compiling issue: “no matching function for call” 在 Linux 中出现函数参数 (C++) 的未声明标识符错误,但在 Windows 中没有 - Got undeclared identifier error for a function argument (C++) in Linux, but not in Windows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM