pthread programming eats up resources (My or FreeBSD's fault?)
David Xu
davidxu at freebsd.org
Thu Feb 20 05:40:34 UTC 2014
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 <stdio.h>
> #include <unistd.h>
> #include <pthread.h>
>
> 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 <stdio.h>
> #include <unistd.h>
> #include <pthread.h>
>
> #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
More information about the freebsd-hackers
mailing list