2017-05-21 23:40:55 +02:00
|
|
|
#include <unistd.h>
|
2017-05-19 13:45:13 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
2017-05-20 00:56:11 +02:00
|
|
|
#include <malloc.h>
|
2017-05-21 23:40:55 +02:00
|
|
|
#include <pthread.h>
|
2017-05-19 13:45:13 +02:00
|
|
|
|
|
|
|
#ifndef NUM_THREADS
|
|
|
|
#define NUM_THREADS 3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef NUM_ITER
|
|
|
|
#define NUM_ITER 10000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SIZE
|
|
|
|
#define SIZE 16384
|
|
|
|
#endif
|
|
|
|
|
2017-05-20 00:56:11 +02:00
|
|
|
__thread void* buf;
|
|
|
|
|
2017-05-21 23:40:55 +02:00
|
|
|
static void* perform_work( void* argument )
|
2017-05-19 13:45:13 +02:00
|
|
|
{
|
|
|
|
int passed_in_value;
|
|
|
|
|
|
|
|
passed_in_value = *( ( int* )argument );
|
2017-05-21 23:40:55 +02:00
|
|
|
printf( "Hello World! It's me, thread %d with argument %d!\n", getpid(), passed_in_value );
|
2017-05-19 13:45:13 +02:00
|
|
|
|
|
|
|
/* optionally: insert more useful stuff here */
|
2017-05-20 00:56:11 +02:00
|
|
|
for(int i=0; i<NUM_ITER; i++)
|
2017-05-19 13:45:13 +02:00
|
|
|
{
|
|
|
|
buf = malloc(SIZE*i);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
malloc_stats();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
|
|
|
pthread_t threads[ NUM_THREADS ];
|
|
|
|
int thread_args[ NUM_THREADS ];
|
|
|
|
int result_code;
|
|
|
|
unsigned index;
|
|
|
|
|
|
|
|
// create all threads one by one
|
|
|
|
for( index = 0; index < NUM_THREADS; ++index )
|
|
|
|
{
|
|
|
|
thread_args[ index ] = index;
|
|
|
|
printf("In main: creating thread %d\n", index);
|
2017-05-21 23:40:55 +02:00
|
|
|
result_code = pthread_create( threads + index, NULL, perform_work, &thread_args[index] );
|
2017-05-19 13:45:13 +02:00
|
|
|
assert( !result_code );
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for each thread to complete
|
|
|
|
for( index = 0; index < NUM_THREADS; ++index )
|
|
|
|
{
|
|
|
|
// block until thread 'index' completes
|
|
|
|
result_code = pthread_join( threads[ index ], NULL );
|
|
|
|
assert( !result_code );
|
|
|
|
printf( "In main: thread %d has completed\n", index );
|
|
|
|
}
|
|
|
|
|
|
|
|
printf( "In main: All threads completed successfully\n" );
|
|
|
|
exit( EXIT_SUCCESS );
|
|
|
|
}
|