[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#325600: <defunct> threads.... a solution?



Daniel Jacobowitz wrote:
On Tue, Oct 25, 2005 at 12:05:16AM -0400, Tom Evans wrote

Do you have a clear testcase for this problem?  I tried to reproduce
it, and could not

Well, any threaded program should do, but while working on the solution, I used the test below. Compile if and run it and in another window look for the process - you;ll see lots of defunct threads.

This is what I used:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void*
pthread_explicit_exit( void * arg );

void
my_clean( void * arg )
{
 printf( "clean\n" );
}


void*
child( void *arg )
{
 int retval = 0;

 struct _pthread_cleanup_buffer _buffer;

 _pthread_cleanup_push (&_buffer, (my_clean), (0));

 printf( "child\n" );
 pthread_exit( (void *)&retval );
}

void*
pthread_implicit_exit( void *arg )
{
 int retval = 0;
 pthread_t imp, exp;

 struct _pthread_cleanup_buffer _buffer;

 _pthread_cleanup_push (&_buffer, (my_clean), (0));

 printf( "implicit\n" );

 pthread_create( &imp, NULL, child, NULL );
 pthread_detach( imp );
 pthread_create( &exp, NULL, child, NULL );
 pthread_detach( exp );

 pthread_exit( (void *)&retval );
}

void*
pthread_explicit_exit( void * arg )
{
 pthread_t imp, exp;

 static int retval = 0;

 struct _pthread_cleanup_buffer _buffer;

 _pthread_cleanup_push (&_buffer, (my_clean), (0));


 printf( "explicit\n" );

 pthread_create( &imp, NULL, child, NULL );
 pthread_detach( imp );
 pthread_create( &exp, NULL, child, NULL );
 pthread_detach( exp );

  pthread_exit( (void *)&retval );
}

int
main( int argc, char *argv[] )
{
 pthread_t imp, exp;

 struct _pthread_cleanup_buffer _buffer;

 _pthread_cleanup_push (&_buffer, (my_clean), (0));
 pthread_create( &imp, NULL, pthread_implicit_exit, NULL );
 pthread_detach( imp );
 //  pthread_create( &exp, NULL, pthread_explicit_exit, NULL );
 //  pthread_detach( exp );

 while ( 1 ) {
   sleep (10);
 }

 return 0;
}



Reply to: