From owner-freebsd-hackers@FreeBSD.ORG Tue Nov 28 22:02:13 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 78C8416A40F for ; Tue, 28 Nov 2006 22:02:13 +0000 (UTC) (envelope-from pieter@degoeje.nl) Received: from smtp.utwente.nl (smtp2.utsp.utwente.nl [130.89.2.9]) by mx1.FreeBSD.org (Postfix) with ESMTP id 1E47F43CAA for ; Tue, 28 Nov 2006 22:02:07 +0000 (GMT) (envelope-from pieter@degoeje.nl) Received: from nox.student.utwente.nl (nox.student.utwente.nl [130.89.165.91]) by smtp.utwente.nl (8.12.10/SuSE Linux 0.7) with ESMTP id kASM28ox003282 for ; Tue, 28 Nov 2006 23:02:09 +0100 From: Pieter de Goeje To: freebsd-hackers@freebsd.org Date: Tue, 28 Nov 2006 23:02:08 +0100 User-Agent: KMail/1.9.4 References: <456CAB12.9070507@u.washington.edu> In-Reply-To: <456CAB12.9070507@u.washington.edu> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200611282302.08439.pieter@degoeje.nl> X-UTwente-MailScanner-Information: Scanned by MailScanner. Contact helpdesk@ITBE.utwente.nl for more information. X-UTwente-MailScanner: Found to be clean X-UTwente-MailScanner-From: pieter@degoeje.nl X-Spam-Status: No Subject: Re: pthreads : questions about concurrency and lifetime X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 28 Nov 2006 22:02:13 -0000 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