#include #include #include #include void firstThread(int *); void secondThread(int *); static char rcsid[] = "$Id: index.html,v 1.6 2005/05/18 00:02:55 rcampos Exp rcampos $"; int main(void) { pthread_t t1, t2; int argt1 = 0, argt2 = 0; int rc; /* int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg); */ /* Create first thread, rc will hold the return code for pthread_create */ rc = pthread_create(&t1, NULL, (void *)firstThread, (void *) &argt1); /* pthread_create should return 0, otherwise something is wrong */ if (rc != 0) { perror("main: error creating first thread"); exit(1); } /* Create the secod thread, same as above only changing some parameters */ rc = pthread_create(&t2, NULL, (void *)secondThread, (void *) &argt2); if (rc != 0) { perror("main: error creating second thread"); exit(1); } /* int pthread_join(pthread_t thread, void **value_ptr); */ /* Wait for the first thread to finish */ rc = pthread_join(t1, NULL); if (rc != 0) { perror("main: error joining first thread"); exit(1); } else { printf("main: first thread returned successfully\n"); } /* Wait for the second thread to finish */ rc = pthread_join(t2, NULL); if (rc != 0) { perror("main: error joining second thread"); exit(1); } else { printf("main: second thread returned successfully\n"); } printf("main: argt1 = %d, argt2 = %d\n", argt1, argt2); return 0; } void firstThread(int *arg) { /* This routine adds 30 to a given arg */ pthread_t myId; int i; myId = pthread_self(); printf("firstThread: my ID is %d\n", myId); (*arg) += 30; } void secondThread(int *arg) { /* This routine subtracts 10 from a given arg */ pthread_t myId; int i; myId = pthread_self(); printf("secondThread: my ID is %d\n", myId); (*arg) -= 10; }