From owner-freebsd-hackers@FreeBSD.ORG Thu Feb 20 05:40:34 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 31C881A1 for ; Thu, 20 Feb 2014 05:40:34 +0000 (UTC) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 1C61B13DF; Thu, 20 Feb 2014 05:40:34 +0000 (UTC) Received: from xyf.my.dom (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.7/8.14.7) with ESMTP id s1K5eWPh076193; Thu, 20 Feb 2014 05:40:33 GMT (envelope-from davidxu@freebsd.org) Message-ID: <53059574.8090605@freebsd.org> Date: Thu, 20 Feb 2014 13:41:08 +0800 From: David Xu User-Agent: Mozilla/5.0 (X11; FreeBSD i386; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: Andre Albsmeier , freebsd-hackers@freebsd.org Subject: Re: pthread programming eats up resources (My or FreeBSD's fault?) References: <20140218180646.GA67861@schlappy> In-Reply-To: <20140218180646.GA67861@schlappy> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 Feb 2014 05:40:34 -0000 On 2014/02/19 02:06, Andre Albsmeier wrote: > Well, as these are my first steps regarding thread programming, > it's probably my fault... > > Why does this programme slowly grow and grow until it hits > resource limits? > > ----- snip pth1.c ----- > > #include > #include > #include > > void* mythread( void* arg ) > { > return NULL; > } > > int main( int argc, const char* const argv[] ) > { > pthread_t pthr; > int i; > > while( 1 ) { > > for( i=1000; i; i-- ) > if( pthread_create( &pthr, NULL, mythread, NULL ) != 0 ) > fprintf( stderr, "pthread_create\n" ); > else > pthread_detach( pthr ); > > putchar( '.' ); > fflush( stdout ); > usleep( 25000 ); > } > } > > ----- snap ----- > > Just to be sure I have also created the non-detaching version > which behaves in the same way: > > ----- snip pth2.c ----- > > #include > #include > #include > > #define M 1000 > > pthread_t pthr[M]; > > void* mythread( void* arg ) > { > return NULL; > } > > int main( int argc, const char* const argv[] ) > { > int i; > > while( 1 ) { > > for( i=M; i; i-- ) > if( pthread_create( &pthr[i], NULL, mythread, NULL ) != 0 ) > fprintf( stderr, "pthread_create\n" ); > > for( i=M; i; i-- ) > if( pthread_join( pthr[i], NULL ) != 0 ) > fprintf( stderr, "pthread_join\n" ); > > putchar( '.' ); > fflush( stdout ); > usleep( 25000 ); > } > } > > ----- snap ----- > > Compile them using -pthread and watch their ps output in another > window (FreeBSD-9.2 but that shouldn't matter). > > So what am I doing wrong here? > > Thanks, > > -Andre please compile it as static binary and run it, check if the problem still exists, I am hunting the bug, it is not necessary in the libthr because I have not changed its code for a long time. Regards, David Xu