Is it OK to call pthread_exit from main?

Well its definately legal in the linux implementation of pthreads, see the notes section in pthreads_exit It states.

Well its definately legal in the linux implementation of pthreads, see the notes section in pthreads_exit. It states To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3). 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.

Then I guess it should be legal in the win32 implementation (sourceware. Org/pthreads-win32/bugs. Html) too.

I've been looking for a known bug that explains this behavior, but I couldn't find it. In my opinion, either this is a buggy behavior or there's an actual reason I am not supposed to call pthread_exit on pthreads_win32. Can anybody confirm any of these hypothesis?

– user429788 Aug 24 '10 at 18:52 @matasierra: I've added some more detail to the answer aboce. Also, what is main actually executing? – torak Aug 24 '10 at 19:07 The first printf is actually printed out, but the second one is not (as expected).

The problem is that the program will not terminate. It simply gets kind of frozen. – user429788 Aug 24 '10 at 19:25 I finally found the problem.

I think it was some kind of malware on my operating system. I've just cleaned it up with an antispyware tool and now it works as expected. Oh, my... I feel really stupid.

@torak I'm going to accept your answer. Thanks for your help! – user429788 Aug 24 '10 at 19:45.

This completely legal and intended behavior. The whole process only ends when either all threads terminate or exit is called explicitly or implicitly. A normal return from main is equivalent to a call to exit.

If you end main with pthread_exit your are saying explicitly that you want the other threads to continue.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions