从 main 调用 pthread_exit 可以吗?

2023-12-08

当我打电话时pthread_exit from main,程序永远不会终止。我希望程序能够完成,因为我正在退出程序的唯一线程,但它不起作用。看来挂了

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

int main(int argc, char *argv[])
{
    printf("-one-\n");

    pthread_exit(NULL);

    printf("-two-\n");
}

流程浏览器显示(唯一的)线程位于Wait:DelayExecution state.

根据pthread_exit文档:

该进程应退出并退出 最后一个线程结束后状态为 0 已被终止。该行为应是 就像调用的实现一样 exit() 在线程中参数为零 终止时间。

我在用着开发-C++ v4.9.9.2 and pthreads-win32 v2.8.0.0(链接到libpthreadGC2.a).

该库似乎没问题(例如,调用pthread_self or pthread_create from main工作正常)。

有什么理由我不应该打电话吗pthread_exit from main?


好吧,这在 pthreads 的 Linux 实现中绝对是合法的,请参阅中的注释部分线程退出。它指出

为了让其他线程继续执行,主线程应该终止 通过调用 pthread_exit() 而不是 exit(3)。

进一步看一下源码here(torwads the end) 表明它大致翻译为 _endthread 或 _endthreadex。文档here对于那些没有提到不在初始线程中调用它的人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 main 调用 pthread_exit 可以吗? 的相关文章

随机推荐