From owner-freebsd-hackers@FreeBSD.ORG Tue Feb 18 18:09:40 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 E743BF55 for ; Tue, 18 Feb 2014 18:09:40 +0000 (UTC) Received: from mail.myota.org (mail.myota.org [85.10.206.105]) by mx1.freebsd.org (Postfix) with ESMTP id 7831F12A8 for ; Tue, 18 Feb 2014 18:09:39 +0000 (UTC) Received: from mobile.client (192.133.167.190.d.dyn.codetel.net.do [190.167.133.192] (may be forged)) (authenticated bits=128) by mail.myota.org (8.14.7/8.14.7) with ESMTP id s1II6q83057170; Tue, 18 Feb 2014 19:06:57 +0100 (CET) (envelope-from mail@ma17.ata.myota.org) Received: from submit.client ([127.0.0.1]) by schlappy.local (8.14.7/8.14.7) with ESMTP id s1II6k11070595; Tue, 18 Feb 2014 19:06:48 +0100 (CET) (envelope-from mail@ma17.ata.myota.org) Received: (from mu@schlappy.local) by schlappy.local (8.14.7/8.14.7/Submit) id s1II6kX3070594; Tue, 18 Feb 2014 19:06:46 +0100 (CET) (envelope-from mail@ma17.ata.myota.org) Date: Tue, 18 Feb 2014 19:06:46 +0100 From: Andre Albsmeier To: freebsd-hackers@freebsd.org Subject: pthread programming eats up resources (My or FreeBSD's fault?) Message-ID: <20140218180646.GA67861@schlappy> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) X-Greylist: Not delayed on 85.10.206.105, ACL: AUTH(59), Origin: DO, OS: FreeBSD 9.x X-Virus-Scanned: clamav-milter 0.98.1 at colo X-Virus-Status: Clean Cc: mail@ma17.ata.myota.org 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: Tue, 18 Feb 2014 18:09:41 -0000 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