// Pthreads example code // compile with: gcc -Wall pthreads_example.c -o pthreads_example -lpthread // execute with: ./pthreads_example (or "time ./pthreads_example") #include #include void thread_func (int num) { long long i,j; printf("Thread %d executing.\n", num); j = 0; for (i=0; i < 700000000U; ++i){ ++j; } printf("Thread %d done (%lld).\n", num, j); } int main () { pthread_t thread1, thread2; pthread_t thread3, thread4; pthread_create(&thread1, NULL, (void *) &thread_func, (void *) 1); pthread_create(&thread2, NULL, (void *) &thread_func, (void *) 2); pthread_create(&thread3, NULL, (void *) &thread_func, (void *) 3); pthread_create(&thread4, NULL, (void *) &thread_func, (void *) 4); pthread_join(thread1, NULL); pthread_join(thread2, NULL); pthread_join(thread3, NULL); pthread_join(thread4, NULL); return 0; }