Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 28 Nov 2006 14:41:13 -0800
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        freebsd-hackers@freebsd.org
Subject:   Re: pthreads : questions about concurrency and lifetime
Message-ID:  <456CBB09.10804@u.washington.edu>
In-Reply-To: <200611282302.08439.pieter@degoeje.nl>
References:  <456CAB12.9070507@u.washington.edu> <200611282302.08439.pieter@degoeje.nl>

next in thread | previous in thread | raw e-mail | index | archive | help
Pieter de Goeje wrote:
> On Tuesday 28 November 2006 22:33, Garrett Cooper wrote:
>   
>> Hello once again,
>>     Just wondering about pthreads now. I know that the lifetime (scope)
>> of a regular procedural function in C is simple.. it's from the top of
>> the function body to the bottom of the function body (assuming no
>> infinite loops are injected). Example:
>>
>> (void*) function(void*) {/* lifetime of function is here. */ }
>>
>> However looking over pthread(3), there are a number of different
>> functions for killing threads and exiting child threads, in order
>> terminate child threads (and maybe to get back to the main thread of
>> execution in a program).
>>
>> So my question is, once the end of a function body is reached that was
>> made using pthread_create(), does the thread exit and 'destroy' itself
>> or do I need to do 'manual' cleanup, i.e. run pthread_detach(3),
>> pthread_exit(3), or pthread_kill(3)?
>>     
>
> There are three ways to cleanup a thread:
> 1) pthread_detach(3)
> 2) pthread_join(3)
> 3) creating a thread with PTHREAD_CREATE_DETACHED attribute set, see 
> pthread_attr_setdetachstate(3)
>
> Calling pthread_detach(3) directly after you created the thread is an easy way 
> to create an "uncontrolled" thread. Effectively the same as #3. The thread 
> will cleanup automatically after the thread function returns.
>
> -- Pieter de Goeje
>   
So that means no, after a function's definition is reached the 
thread/resources stay in a semi-'alive' (maybe 'zombified') state?, or 
does the kernel cleanup / reclaim all of the resources tied up with the 
thread?
-Garrett



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?456CBB09.10804>