在现代 POSIX 环境中,除了 usleep 之外,我还能使用什么?
我对 C 相当陌生,但正在编写一个小型多线程应用程序。我想给线程引入延迟。我一直在使用“usleep”,其行为正是我想要的 - 但它会在 C99 中生成警告。
函数“usleep”的隐式声明
这只是一个警告,但它让我烦恼。我在 Google 上搜索了答案,但我所能找到的只是一种 while 循环/计时器方法,看起来它会占用 CPU 资源。
编辑:
我的包括:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
并且我正在调用编译器:
c99 program.c -Wall -pedantic -W -lpthread
编辑#2:
我创建了一个新文件,其中包含:
#include <unistd.h>
int main(void) {
usleep(10);
}
并且我仍然收到警告。
编辑#3: 根据建议,我已经更新了问题的文本。
I'm fairly new to C but writing a small multithreaded application. I want to introduce a delay to a thread. I'd been using 'usleep' and the behavior is what I desire - but it generates warnings in C99.
implicit declaration of function ‘usleep’
It's only a warning, but it bothers me. I've Googled for an answer but all I could find was a while-loop / timer approach that seemed like it would be CPU intensive.
EDIT:
My includes are:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
And I'm invoking the compiler with:
c99 program.c -Wall -pedantic -W -lpthread
EDIT #2:
I've created a new file that contains:
#include <unistd.h>
int main(void) {
usleep(10);
}
And I still get the warning.
EDIT #3:
As suggested, I've updated the text of the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是您正在使用标准 C99 编译器,但您正在尝试访问 POSIX 扩展。要公开 POSIX 扩展,您应该将 _POSIX_C_SOURCE 定义为 200809L(对于当前标准)。例如,此程序:
使用以下编译命令将正确编译并等待 1.5 秒(1 秒 + 500000000 纳秒):
_POSIX_C_SOURCE 宏必须使用适当的值定义,以使 POSIX 扩展可用。
另外,选项 -Wall、-pedantic 和 -W 没有为 POSIX c99 命令定义,这些对我来说看起来更像 gcc 命令(如果它们在您的系统上工作,那就很好,只是要注意它们不能移植到其他 POSIX系统)。
The problem is that you are using a standard C99 compiler, but you're trying to access POSIX extensions. To expose the POSIX extensions you should for example define _POSIX_C_SOURCE to 200809L (for the current standard). For example this program:
will compile correctly and wait for 1.5 seconds (1 second + 500000000 nanoseconds) with the following compilation command:
The _POSIX_C_SOURCE macro must be defined with an appropriate value for the POSIX extensions to be available.
Also the options -Wall, -pedantic and -W are not defined for the POSIX c99 command, those look more like gcc commands to me (if they work on your system then that's fine, just be aware that they are not portable to other POSIX systems).
您可能使用的是现代 POSIX 系统:
在我的系统(linux)上,有一个必须设置才能恢复该功能的宏的详细解释。但你应该遵循 zvrba 已经给出的建议,使用
nanosleep
代替。You are probably on a modern POSIX system:
On my system (linux) there is a detailed explanation of the macros that must be set to get that function back. But you should just follow the advice that zvrba already gave, use
nanosleep
instead.从手册页来看:此功能已过时。使用 nanosleep 代替。
From the manual page: This function is obsolete. Use nanosleep instead.
此警告通常意味着您没有
#include
正确的头文件,在本例中unistd.h
。This warning usually means that you didn't
#include
the right header file, in this caseunistd.h
.您有
#include
吗?您可以使用一些类似的方法来代替:nanosleep() 表示纳秒,sleep() 表示秒。或者另一种方法可以使用clock(),但我认为这会浪费更多的CPU。
Did you have the
#include <unistd.h>
?.And you can use some of the similar methods instead: nanosleep() for nanoseconds and sleep() for seconds. Or another way could be using clock(), but i think this is more CPU wasting.