#include #include static int shared_counter = 0; static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; static void *worker(void *arg) { (void)arg; (void)arg; for (int i = 0; i < 5000; ++i) { pthread_mutex_lock(&mtx); ++shared_counter; pthread_mutex_unlock(&mtx); } 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("Shared counter is %d\n", shared_counter); return 0; }