#include #include #include #define N 1000 sem_t lock; int shared = 0; void *inc(void *arg) { (void)arg; for (int i = 0; i < N; i++) { sem_wait(&lock); shared++; sem_post(&lock); } return NULL; } int main(void) { pthread_t t[2]; sem_init(&lock, 0, 1); for (int i = 0; i < 2; i++) { pthread_create(&t[i], NULL, inc, NULL); } for (int i = 0; i < 2; i++) { pthread_join(t[i], NULL); } printf("%d\n", shared); sem_destroy(&lock); return 0; }