[haiku-development] Re: Calling pthread_join on a detached pthread

  • From: "Ingo Weinhold" <ingo_weinhold@xxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Sun, 05 Aug 2012 18:28:28 +0200

Jason Whitehorn wrote:
> This is regarding an issue I submitted (https://dev.haiku-os.org/ticket/8836).
> Being new around here, I respect (and value) you all's opinion. I just
> wanted to better understand the issues at hand, and perhaps see about
> better focusing my efforts. I figured an email conversation was preferred
> to cluttering up the ticket with "conversation".
> 
> In regard's to Bonefish's comment about "leaking memory"; perhaps this is
> just my ignorance, but I am a little confused about not calling free in
> this case would leak memory, as it would appear that it was the call to
> free that was actually crashing.

This is how our implementation works: For the thread, as long as it lives, 
there exists an object (a heap-allocated structure) representing it. The 
difference between a detached and a joinable thread is when that object is 
deleted. For a detached thread the object is solely owned by the thread itself 
and it is deleted as soon as the thread dies. For a joinable thread the object 
is not owned by the thread. Instead the creator of the thread is responsible 
for calling pthread_join(), which will delete the object when the thread has 
died.

So, if you call pthread_join() on a detached thread, the following two 
scenarios are possible:

1. The thread has already quit and thus deleted the object. As a consequence 
the pthread_join() implementation would access already free memory. The values 
read from it cannot be trusted, since it might already be reused. In fact the 
memory could have been returned to the kernel, in which case accessing it would 
already crash immediately.

2. The thread is still running. In this case the object would still be valid 
and your patch would work.

Which scenario is met is a race condition. Code calling pthread_join() on a 
detached thread is just broken.

> I guess I was using an outdated test (
> http://sourceforge.net/projects/posixtest/). Is there a preferred POSIX
> compatibility test?

I'm not aware of any alternative. AFAIK the big open source OSs (Linux, 
FreeBSD) have their own test suites, which don't only test POSIX compliance, 
though. Unfortunately the open POSIX test suite on SourceForge hasn't been work 
on for years. Their mission statement is to create a POSIX 2001 test suite, 
which as of now is two iterations of the standard behind. I have imported the 
last version of the POSIX test suite into our repository [1] and removed 
several tests that aren't useful and updated a few others. Ideally we would 
update and extend that test suite to be up to date and complete with respect to 
the current standard.

CU, Ingo

[1] 
http://cgit.haiku-os.org/haiku/tree/src/tests/system/libroot/posix/posixtestsuite

Other related posts: