mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: pthread_join stuck in infinite loop
Date: Sun, 20 Sep 2015 14:33:31 -0400	[thread overview]
Message-ID: <20150920183331.GN17773@brightrain.aerifal.cx> (raw)
In-Reply-To: <B9275837-6376-4749-8179-E203DC48E230@gmail.com>

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


      parent reply	other threads:[~2015-09-20 18:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-20 18:07 Julien Ramseier
2015-09-20 18:29 ` Szabolcs Nagy
2015-09-20 18:41   ` Jens Gustedt
2015-09-21  9:00     ` Julien Ramseier
2015-09-20 18:33 ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150920183331.GN17773@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).