MIT-Curricular/OS/C/theory/sync/withoutsemaphores.c

22 lines
498 B
C

#include <stdio.h>
#include <pthread.h>
int counter = 0;
void* increment(void* arg) {
for (int i = 0; i < 1000000; i++) {
counter++; // No synchronization, will lead to race condition
}
return NULL;
}
int main() {
pthread_t t1, t2;
pthread_create(&t1, NULL, increment, NULL);
pthread_create(&t2, NULL, increment, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("Final Counter Value (without semaphores): %d\n", counter);
return 0;
}