使用 POSIX 定时器后程序不退出
考虑以下程序:
#define _POSIX_C_SOURCE 200809L
#include <time.h>
#include <pthread.h>
#include <signal.h>
void timerfunc(union sigval val) { }
int main()
{
struct sigevent sev = { .sigev_notify = SIGEV_THREAD,
.sigev_notify_function = timerfunc };
timer_t t;
timer_create(CLOCK_REALTIME, &sev, &t);
timer_delete(t);
pthread_exit(0);
}
与 glibc 链接,它不仅无法终止,而且除了 kill -9
/SIGKILL
之外无法终止。标准允许这种行为吗?除了始终显式退出进程(而不是仅退出所有线程)之外,是否还有好的解决方法?
Consider the following program:
#define _POSIX_C_SOURCE 200809L
#include <time.h>
#include <pthread.h>
#include <signal.h>
void timerfunc(union sigval val) { }
int main()
{
struct sigevent sev = { .sigev_notify = SIGEV_THREAD,
.sigev_notify_function = timerfunc };
timer_t t;
timer_create(CLOCK_REALTIME, &sev, &t);
timer_delete(t);
pthread_exit(0);
}
Linked with glibc, it not only fails to terminate, but it is unkillable except by kill -9
/SIGKILL
. Is this behavior permitted by the standard? Are there good workarounds aside from either always explicitly exiting the process (as opposed to just exiting all threads)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,POSIX 特别指出
这意味着允许任何生命周期。
SIGEV_THREAD
是一种糟糕的魔力,应该避免。Well, POSIX specifically says it is
which implies that any lifetime is allowed.
SIGEV_THREAD
is simply bad mojo, and should be avoided.pthread_exit 是否与主进程线程一起工作?我一直想知道 pthread 函数是否可以在不是由 pthread_create 创建的线程上工作。
Does pthread_exit work with the main process thread? I've always wondered if pthread functions work on threads not created by pthread_create.