Is it valid to call pthread_join on the main thread?

Yes, this is possible. Indeed, this possibility is one of the main reasons why pthread_detach() exists. From the POSIX docs for pthread_detach() (see man pthread_detach ).

Yes, this is possible. Indeed, this possibility is one of the main reasons why pthread_detach() exists. From the POSIX docs for pthread_detach() (see man pthread_detach): It has been suggested that a "detach" function is not necessary; the detachstate thread creation attribute is sufficient, since a thread need never be dynamically detached.

However, need arises in at least two cases: 1. In a cancellation handler for a pthread_join() it is nearly essen- tial to have a pthread_detach() function in order to detach the thread on which pthread_join() was waiting. Without it, it would be necessary to have the handler do another pthread_join() to attempt to detach the thread, which would both delay the cancella- tion processing for an unbounded period and introduce a new call to pthread_join(), which might itself need a cancellation handler.

A dynamic detach is nearly essential in this case. 2. In order to detach the "initial thread" (as may be desirable in processes that set up server threads).

So what you're proposing is fine according to the standards. Edit: Just to confirm that further, the POSIX description of exec() states: The initial thread in the new process image shall be joinable, as if created with the detachstate attribute set to PTHREAD_CREATE_JOINABLE.

I can't imagine why you would do this in a real-world application (someone please comment an example if you can think of one), but I don't believe it is even possible. All of the searches regarding pthreads that I have looked at always have the join being called from the main thread, not a secondary thread. The join also requires that the thread you are trying to join is created with the PTHREAD_CREATE_JOINABLE flag.

I have not been able to find any documentation stating whether or not the main thread is created as such. Codeguru has a similar question on it here which may or may not help clear it up. If what you want to accomplish is the child thread continuing after the main thread has exited, you should create the child thread detached, or use fork/vfork instead, depending on what platform you're on.

PTHREAD_CREATE_JOINABLE is the default according to POSIX; you don't need to use a pthread_attr_t to obtain it. – R.. Nov 19 '10 at 19:08 By the way, I agree that this is probably not a good practice. I asked the question from a standpoint of whether an implementation must support this dubious usage, not whether an application writer should use it.

:-) – R.. Nov 19 '10 at 19:18.

Your approach looks formally valid to me, in particular since you are doing the pthread_exit at the end of main. On the other hand I didn't find any indication in POSIX whether or not the initial thread of main should be joinable or not. The rationale in psmears' answer only asks that main should be detachable whence it is created joinable.

Also something else malicious might call pthread_detach from a subroutine called by main or so. So you should definitively check the return value of pthread_join to check for such a case. Also in your example code you have a race between the creation of start and the termination of main: main might be terminated before start arrives at pthread_join Mutexing the access to the mt variable should capure that.

If pthread_detach has already been called on a thread, the result of calling pthread_join on that thread is undefined; it need not return any error, and in fact it might crash if the thread has already terminated and its resources were deallocated. – R.. Nov 19 '10 at 22:43 Assuming the main thread is joinable, there's no problem if main calls pthread_exit before start calls pthread_join. The semantics of a joinable thread are like those of a pid for a child process.

Just like the pid is reserved (zombie process) until you wait for it and collect its exit status, the pthread_t is valid until you detach or join the thread, even if it has already exited. – R.. Nov 19 '10 at 22:45 I don't agree with your first remark. A non-joinable thread is detectable and doesn't lead to an undefined result.

The pthread_join() function shall fail if: *EINVAL* The implementation has detected that the value specified by thread does not refer to a joinable thread. – Jens Gustedt Nov 20 '10 at 7:22 1 R. Is correct.

The wording of the standard is confusing - but it says that pthread_join() shall fail if the implementation detects an invalid thread id. It doesn't say that the implementation must be able to detect such an invalid thread id, and indeed glibc on Linux can and will crash in these circumstances: see udrepper.livejournal.com/tag/programming%20posix – psmears Nov 20 '10 at 12:50 POSIX 2008 makes this much more clear and changes all those bogus shall fail statements for undetectable conditions (many of which involve the values of uninitialized variables) to may fail. It's not that unreasonable (although POSIX 2008 makes it UB) to expect pthread_join to return EINVAL on a detached thread if it is still running, but if the thread has terminated, you should expect all hell to break loose.

SIGSEGV if you're lucky, and randomly joining or corrupting a different thread if the thread id got reassigned or memory reallocated for something else. – R.. 08 ma3 at 16:21.

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