2025-02-08 12:08:00 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
void* increment(void* arg) {
|
2025-02-09 03:33:42 +05:30
|
|
|
for (int i = 0; i < 1000000; i++) {
|
|
|
|
counter++; // No synchronization, will lead to race condition
|
2025-02-08 12:08:00 +05:30
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|