pthread programming eats up resources (My or FreeBSD's fault?)

Erich Dollansky erichsfreebsdlist at alogt.com
Thu Feb 20 03:39:20 UTC 2014


Hi,

On Wed, 19 Feb 2014 17:24:41 +0100
Andre Albsmeier <mail at ma17.ata.myota.org> wrote:

I am now running this program:

> #include <stdio.h>
> #include <unistd.h>
> #include <pthread.h>
> 
> #define THREADS 1000
> 
> int c = 0;
> pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
> 
> void* mythread( void* arg )
> {
>   pthread_mutex_lock( &mtx );
>   c--;
>   pthread_mutex_unlock( &mtx );
> 
>   return NULL;
> }
> 
> int main( int argc, const char* const argv[] )
> {
>   pthread_t pthr;
>   int i;
> 
>   while( 1 ) {
> 
>     pthread_mutex_lock( &mtx );
>     c += THREADS;
>     pthread_mutex_unlock( &mtx );
> 
>     for( i=THREADS; i; i-- )
>       if( pthread_create( &pthr, NULL, mythread, NULL ) != 0 )
>         fprintf( stderr, "pthread_create\n" );
>       else
>         pthread_detach( pthr );
> 
>     pthread_mutex_lock( &mtx );
>     fprintf( stderr, "%d:", c );
>     pthread_mutex_unlock( &mtx );
> 
>     usleep( 25000 );
> 
>     pthread_mutex_lock( &mtx );
>     fprintf( stderr, "%d ", c );
>     pthread_mutex_unlock( &mtx );
>   }
> }

It runs on an i7 with two cores plus two hyper threads. The machine has
8GB of RAM and 16GB of Swap. The resident part of the program stays
below 8GB (what wonder) but the rest is swapped out.

I run this program on 10.0 from some 2 weeks ago.

As already said, it looks like there stays some memory left after a
thread terminates.

To say something positive, the program really cleaned the memory.

Erich


More information about the freebsd-hackers mailing list