开发者

Program not exiting after using POSIX timers

开发者 https://www.devze.com 2023-02-22 05:56 出处:网络
Consider the following program: #define _POSIX_C_SOURCE 200809L #include <time.h> #include <pthread.h>

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 开发者_Go百科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)?


Well, POSIX specifically says it is

...impossible to determine the lifetime of the created thread...

which implies that any lifetime is allowed.

SIGEV_THREAD is simply bad mojo, and should be avoided.


Does pthread_exit work with the main process thread? I've always wondered if pthread functions work on threads not created by pthread_create.

0

精彩评论

暂无评论...
验证码 换一张
取 消