/* Shaun Cooper September 26, 2000 Simple thread example to demonstrate how a thread is invoked. Note: You must compile this code with certain flags gcc file.c -lpthread Relevant manual pages (via google) are: pthread pthread_create pthread_join */ #include #include /* define a structure so that we can provide 2 values to the function (thread) */ typedef struct {int val1, val2;} input; /* simple function which outputs the values of the structures 4 times */ void f( input * j ) { int i; for(i=0;i<=4;i++) { printf ("in f, val1 is %d and val2 is %d\n",(*j).val1,(*j).val2); } } /* simple example of a thread call */ main() { int i; int j; input in; pthread_t t_child1; int status1; in.val1=20; in.val2=30; /* this creates a thread by calling the function "f". The input to f really is 2 values, so we pack them into a structure and provide a pointer to the stricture */ if ((j=pthread_create(&t_child1,NULL,(void *)f,&in)) != 0 ) { printf("Cannot create thread %d\n",j); exit(1); } /* this waits on the thread to finish */ pthread_join(t_child1,(void **) &status1); }