是否确定,从主调用了pthread_exit? [英] Is it OK to call pthread_exit from main?
问题描述
当我称之为了pthread_exit
从主
,程序永远不会终止。我期望程序完成,因为我是在退出程序的唯一线程,但它不工作。似乎挂起。
When I call pthread_exit
from main
, the program never gets to terminate. I expected the program to finish, since I was exiting the program's only thread, but it doesn't work. It seems hung.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int main(int argc, char *argv[])
{
printf("-one-\n");
pthread_exit(NULL);
printf("-two-\n");
}
的Process Explorer 显示,(仅)线程处于等待。DelayExecution
状态
Process Explorer shows that the (only) thread is in Wait:DelayExecution
state.
据了pthread_exit
文件:
这个过程应退出退出
最后一个线程后0地位
被终止。该行为应
好象叫实施
出口()与螺纹零参数
终止时间。
The process shall exit with an exit status of 0 after the last thread has been terminated. The behavior shall be as if the implementation called exit() with a zero argument at thread termination time.
我使用的开发-C ++ v4.9.9.2 和的pthreads-win32的v2.8.0.0 (针对联 libpthreadGC2.a
)。
I'm using Dev-C++ v4.9.9.2 and pthreads-win32 v2.8.0.0 (linking against libpthreadGC2.a
).
该库似乎是确定(例如,调用 pthread_self
或在pthread_create
从主
正常工作)。
The library seems to be OK (for example, calling pthread_self
or pthread_create
from main
works fine).
有什么理由什么我不应该叫了pthread_exit
从主
?
Is there any reason for what I'm not supposed to call pthread_exit
from main
?
推荐答案
好了肯定的法律在Linux实现的pthreads的,看到的 pthreads_exit 。它指出
Well its definately legal in the linux implementation of pthreads, see the notes section in pthreads_exit. It states
要允许其他线程继续执行,主线程应该终止
通过调用pthread_exit(),而不是出口(3)。
To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).
此外,看看源$ C $ C <一个href=\"http://sourceware.org/cgi-bin/cvsweb.cgi/pthreads/pthread_exit.c?rev=1.9&content-type=text/x-cvsweb-markup&cvsroot=pthreads-win32\">here (torwads月底)显示,它大致翻译_endthread或_endthreadex。这里这些文档只字不提在不调用它的初始线程。
Further, a look at the source code here (torwads the end) shows that it roughly translates to _endthread or _endthreadex. The documentation here for those makes no mention of not calling it in the initial thread.
这篇关于是否确定,从主调用了pthread_exit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!