C++ 函数在 windows 中完美运行但在 linux 中运行不完美?
C++ function runs in windows perfectly but not linux?
我正在尝试编写一个简单的 c++ 函数 sleep(int millisecond)
,它将使程序休眠用户特定的毫秒数。
这是我的代码:
#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;
}
当我 运行 此代码在 windows 但在 Linux 上不起作用时,sleep()
功能完美运行。任何人都可以找出我的代码有什么问题吗?
Linux 上的毫秒没有标准 C API,因此您将不得不使用 usleep
。 POSIX sleep
需要几秒钟。
对于 C++11,您可以使用 sleep_for
。
#include <chrono>
#include <thread>
void sleep(unsigned int mseconds) {
std::chrono::milliseconds dura( mseconds);
std::this_thread::sleep_for( dura );
}
我不知道为什么每个人都在围绕你的问题跳舞而不是回答它。
您正在尝试实现您自己的类睡眠功能和您的实现,而它确实忙于等待而不是在内核空间中休眠(这意味着处理器将 "actively" 运行ning 代码让你的程序休眠,而不是告诉机器你的程序正在休眠,它应该 运行 其他代码),就可以了。
问题是 clock()
不需要 return 毫秒。 clock()
将 return 系统 time/process 从纪元开始经过的时间(以滴答计)。该时间的单位取决于实施。
例如,在我的机器上,手册页是这样说的:
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.
RETURN VALUES
The clock() function returns the amount of time used unless an error
occurs, in which case the return value is -1.
SEE ALSO
getrusage(2), clocks(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.
从粗体部分可以看出,滴答是百万分之一秒,也就是微秒(不是毫秒)。要 "sleep" 3 秒,您需要拨打 sleep(3000000)
而不是 sleep(3000)
。
您可以使用内置 sleep()
函数,它以秒而不是毫秒为单位,并且必须包含 unistd.h
标准库,因为内置 sleep()
函数在下面定义这个图书馆。
试一试:
#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
我正在尝试编写一个简单的 c++ 函数 sleep(int millisecond)
,它将使程序休眠用户特定的毫秒数。
这是我的代码:
#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;
}
当我 运行 此代码在 windows 但在 Linux 上不起作用时,sleep()
功能完美运行。任何人都可以找出我的代码有什么问题吗?
Linux 上的毫秒没有标准 C API,因此您将不得不使用 usleep
。 POSIX sleep
需要几秒钟。
对于 C++11,您可以使用 sleep_for
。
#include <chrono>
#include <thread>
void sleep(unsigned int mseconds) {
std::chrono::milliseconds dura( mseconds);
std::this_thread::sleep_for( dura );
}
我不知道为什么每个人都在围绕你的问题跳舞而不是回答它。
您正在尝试实现您自己的类睡眠功能和您的实现,而它确实忙于等待而不是在内核空间中休眠(这意味着处理器将 "actively" 运行ning 代码让你的程序休眠,而不是告诉机器你的程序正在休眠,它应该 运行 其他代码),就可以了。
问题是 clock()
不需要 return 毫秒。 clock()
将 return 系统 time/process 从纪元开始经过的时间(以滴答计)。该时间的单位取决于实施。
例如,在我的机器上,手册页是这样说的:
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.
RETURN VALUES
The clock() function returns the amount of time used unless an error occurs, in which case the return value is -1.
SEE ALSO
getrusage(2), clocks(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.
从粗体部分可以看出,滴答是百万分之一秒,也就是微秒(不是毫秒)。要 "sleep" 3 秒,您需要拨打 sleep(3000000)
而不是 sleep(3000)
。
您可以使用内置 sleep()
函数,它以秒而不是毫秒为单位,并且必须包含 unistd.h
标准库,因为内置 sleep()
函数在下面定义这个图书馆。
试一试:
#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