/******************************************************************* This is a sample threaded program in C. The main thread creates 4 threads. Each thread simply prints out a message before exiting. Notice that I have set the thread attributes to joinable. Compilation: gcc threads.c -o threads -Wall -lpthread *******************************************************************/ #include #include #define NUM_THREADS 4 void * thread_function (void *arg) { int id = *((int *)arg); printf( "Hello from thread %d!\n", id ); return NULL; } int main() { int i, tmp; int arg[NUM_THREADS] = {0,1,2,3}; pthread_t thread[NUM_THREADS]; pthread_attr_t attr; /* initialize and set the thread attributes */ pthread_attr_init( &attr ); pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); /* creating threads */ for ( i=0; i