the following proposed code:
- cleanly compiles
- performs the desired functionality
- cleans up after itself, even when an error occurs
- properly (using
perror()
) outputs the OPs error message AND the text reason the system thinks the error occurred to stderr
.
- uses the signature:
int main( void )
so no messages from the compiler about unused parameters.
- Since a loop counter cannot be < 0, uses variable type
size_t
for the loop counter.
- properly initializes (to 0) the
pthread_t
array to 0 so can be used for cleanup operations
- Corrects the format specifiers in the calls to
printf()
to expect a variable with type: size_t
- Has the
main()
function properly wait for the threads to exit before the main()
function exits
- incorporates a new function:
cleanup()
to properly cleanup any threads before exiting the program.
- uses appropriate horizontal spacing and vertical spacing for readability and ease of understanding.
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 20
void *PrintHello( void *arg )
{
size_t threadNum = (size_t)arg;
printf( "Hello World! It's me, thread #%lu!\n", threadNum );
pthread_exit( NULL );
}
void cleanup( pthread_t *threads )
{
for( size_t i=0; i<NUM_THREADS; i++ )
{
if( threads[i] )
{
pthread_join( threads[i], NULL );
}
}
}
int main( void )
{
pthread_t threads[ NUM_THREADS ] = {0};
for( size_t t = 0; t < NUM_THREADS; t++ )
{
printf( "In main: creating thread %ld\n", t );
if( pthread_create( &threads[t], NULL, PrintHello, (void *)t ) )
{
perror( "pthread_create() failed" );
cleanup( threads );
exit( EXIT_FAILURE );
}
}
cleanup( threads );
/* Last thing that main() should do */
pthread_exit( NULL );
}
here is the output from a typical run of the proposed code when no errors occur:
In main: creating thread 0
In main: creating thread 1
Hello World! It's me, thread #0!
In main: creating thread 2
In main: creating thread 3
In main: creating thread 4
Hello World! It's me, thread #3!
In main: creating thread 5
Hello World! It's me, thread #4!
In main: creating thread 6
In main: creating thread 7
Hello World! It's me, thread #6!
Hello World! It's me, thread #5!
In main: creating thread 8
Hello World! It's me, thread #7!
In main: creating thread 9
Hello World! It's me, thread #8!
In main: creating thread 10
In main: creating thread 11
In main: creating thread 12
Hello World! It's me, thread #10!
Hello World! It's me, thread #9!
Hello World! It's me, thread #11!
In main: creating thread 13
In main: creating thread 14
Hello World! It's me, thread #13!
In main: creating thread 15
Hello World! It's me, thread #12!
Hello World! It's me, thread #14!
In main: creating thread 16
Hello World! It's me, thread #15!
In main: creating thread 17
In main: creating thread 18
In main: creating thread 19
Hello World! It's me, thread #19!
Hello World! It's me, thread #16!
Hello World! It's me, thread #17!
Hello World! It's me, thread #18!
Hello World! It's me, thread #1!
Hello World! It's me, thread #2!