#include #include int main() { int i, data[5], sum; pthread_t tid[5]; extern void *addfive(void *arg); for (i = 0; i < 5; i++) { data[i] = i * 5; pthread_create(&tid[i], NULL, addfive, &data[i]); } sum = 0; for (i = 0; i < 5; i++) { pthread_join(tid[i], NULL); /* (wait for termination) */ sum += data[i]; } printf("%d\n", sum); return(0); } void *addfive(void *arg) { int i = *(int *)arg; int j, sum = 0; for (j = 0; j < 5; j++) sum += i + j; *(int *)arg = sum; return(NULL); } /* also mentioned: pthread_exit() -- exit this thread (as an alternative to returning from the thread function) -- directly analogous to processes' calling exit() rather than returning from main(). (Whereas if you call the actual exit() instead of pthread_exit(), you will terminate the whole process, i.e. all threads!) pthread_cancel() -- kill another thread by tid */