934cb1
commit 71e6e5e52d7205a6a792bf9b754550a8719db6b1
934cb1
Author: Asim YarKhan <yarkhan@icl.utk.edu>
934cb1
Date:   Wed Feb 25 12:17:40 2015 -0500
934cb1
934cb1
    Sync thread exit in krental_threads.c
934cb1
    
934cb1
    Thanks to William Cohen for this patch and to Phil Mucci for approving it.
934cb1
    
934cb1
    William Cohnen and Michael Petlan noticed that this test can have
934cb1
    threads dangling after the main thread is done.  This patch tracks the
934cb1
    created threads and ensures that they are joined before the code
934cb1
    exits.
934cb1
    
934cb1
    Note: There is still some problem remaining. For example, the following
934cb1
    test will sometimes (maybe 1 of 10 runs) generate an error message.
934cb1
    > ./ctests/krentel_pthreads 8 2000 10
934cb1
    ....
934cb1
    [10] time = 8, count = 38110, iter = 20, rate = 1905500.0/Kiter
934cb1
    PAPI Error: thread->running_eventset == NULL in _papi_pe_dispatch_timer for fd 14!.
934cb1
    [0] time = 8, count = 38161, iter = 20, rate = 1908050.0/Kiter
934cb1
    krentel_pthreads.c               PASSED
934cb1
934cb1
diff --git a/src/ctests/krentel_pthreads.c b/src/ctests/krentel_pthreads.c
934cb1
index 2417976..9fa3e25 100644
934cb1
--- a/src/ctests/krentel_pthreads.c
934cb1
+++ b/src/ctests/krentel_pthreads.c
934cb1
@@ -143,7 +143,7 @@ my_thread( void *v )
934cb1
 int
934cb1
 main( int argc, char **argv )
934cb1
 {
934cb1
-	pthread_t td;
934cb1
+	pthread_t *td = NULL;
934cb1
 	long n;
934cb1
 
934cb1
 	tests_quiet( argc, argv );	/*Set TESTS_QUIET variable */
934cb1
@@ -155,6 +155,10 @@ main( int argc, char **argv )
934cb1
 	if ( argc < 4 || sscanf( argv[3], "%d", &num_threads ) < 1 )
934cb1
 		num_threads = 3;
934cb1
 
934cb1
+	td = malloc((num_threads+1) * sizeof(pthread_t));
934cb1
+	if (!td)
934cb1
+		test_fail( __FILE__, __LINE__, "td malloc failed", 1 );
934cb1
+
934cb1
 	printf( "program_time = %d, threshold = %d, num_threads = %d\n\n",
934cb1
 			program_time, threshold, num_threads );
934cb1
 
934cb1
@@ -171,15 +175,22 @@ main( int argc, char **argv )
934cb1
 	gettimeofday( &start, NULL );
934cb1
 
934cb1
 	for ( n = 1; n <= num_threads; n++ ) {
934cb1
-		if ( pthread_create( &td, NULL, my_thread, ( void * ) n ) != 0 )
934cb1
+		if ( pthread_create( &(td[n]), NULL, my_thread, ( void * ) n ) != 0 )
934cb1
 			test_fail( __FILE__, __LINE__, "pthread create failed", 1 );
934cb1
 	}
934cb1
 
934cb1
 	my_thread( ( void * ) 0 );
934cb1
 
934cb1
+	/* wait for all the threads */
934cb1
+	for ( n = 1; n <= num_threads; n++ ) {
934cb1
+	  	if ( pthread_join( td[n], NULL))
934cb1
+			test_fail( __FILE__, __LINE__, "pthread join failed", 1 );
934cb1
+	}
934cb1
+
934cb1
+	free(td);
934cb1
+
934cb1
 	printf( "done\n" );
934cb1
 
934cb1
 	test_pass( __FILE__, NULL, 0 );
934cb1
-	pthread_exit( NULL );
934cb1
 	return ( 0 );
934cb1
 }