#include #include int balance = 0; pthread_mutex_t mon = PTHREAD_MUTEX_INITIALIZER; void deposit(int n) { pthread_mutex_lock(&mon); balance += n; pthread_mutex_unlock(&mon); } void *worker(void *arg) { (void)arg; for (int i = 0; i < 1000; i++) deposit(1); return NULL; } int main(void) { pthread_t t1, t2; pthread_create(&t1, NULL, worker, NULL); pthread_create(&t2, NULL, worker, NULL); pthread_join(t1, NULL); pthread_join(t2, NULL); printf("%d\n", balance); return 0; }