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