From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/8528 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: pthread_join stuck in infinite loop Date: Sun, 20 Sep 2015 14:33:31 -0400 Message-ID: <20150920183331.GN17773@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1442774031 10737 80.91.229.3 (20 Sep 2015 18:33:51 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 20 Sep 2015 18:33:51 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-8540-gllmg-musl=m.gmane.org@lists.openwall.com Sun Sep 20 20:33:50 2015 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1ZdjQr-0002TO-Dx for gllmg-musl@m.gmane.org; Sun, 20 Sep 2015 20:33:49 +0200 Original-Received: (qmail 27756 invoked by uid 550); 20 Sep 2015 18:33:46 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 27734 invoked from network); 20 Sep 2015 18:33:44 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:8528 Archived-At: On Sun, Sep 20, 2015 at 08:07:28PM +0200, Julien Ramseier wrote: > Hello, > > pthread_join() never returns when calling it on a detached thread. > I would expect it to return EINVAL instead. Calling pthread_join on a detached thread or thread id which is no longer valid results in undefined behavior. You can't do it. See: http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_join.html "The behavior is undefined if the value specified by the thread argument to pthread_join() does not refer to a joinable thread." and: http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_02 "The lifetime of a thread ID ends after the thread terminates if it was created with the detachstate attribute set to PTHREAD_CREATE_DETACHED or if pthread_detach() or pthread_join() has been called for that thread. A conforming implementation is free to reuse a thread ID after its lifetime has ended. If an application attempts to use a thread ID whose lifetime has ended, the behavior is undefined. If a thread is detached, its thread ID is invalid for use as an argument in a call to pthread_detach() or pthread_join()." Note that it's fundamentally impossible for it to be well-defined for thread ids whose lifetimes have ended, and it wouldn't really make sense to have it be defined for still-live detached threads since, unless you perform additional synchronization to ensure this doesn't happen, it's possible that a detached thread's exit could race with your call to pthread_join and cause the id to become invalid. Rich