#include #include #include #include #define THREADS 5 static int sum = 1; static pthread_mutex_t sum_lock = PTHREAD_MUTEX_INITIALIZER; void *updater(void *ptr){ pthread_mutex_lock(&sum_lock); sum = sum + 1; pthread_mutex_unlock(&sum_lock); pthread_exit(NULL); } int main(void){ int i; pthread_t threads[THREADS]; for(i = 0; i < THREADS ; i++) pthread_create(&threads[i],NULL,updater,NULL); for(i = 0; i < THREADS ; i++) pthread_join(threads[i],NULL); fprintf(stderr, "sum = %d\n", sum); exit(EXIT_SUCCESS); }