#include #include #define N 1000 int counter = 0; pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; void *worker(void *arg) { (void)arg; int i; for (i = 0; i < N; i++) { pthread_mutex_lock(&lock); counter++; pthread_mutex_unlock(&lock); } 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("Final counter: %d\n", counter); return 0; }