CPlay/spinlocks/main.c
2025-08-28 14:45:45 +02:00

31 lines
619 B
C

#include <pthread.h>
#include <stdio.h>
#include "spinlock.h"
spinlock_t lock;
int counter = 0;
void *worker(void *arg) {
printf("Hello from %s!\n", (char *)arg);
for (int i = 0; i < 100000; i++) {
acquire(&lock);
counter++;
release(&lock);
}
return NULL;
}
int main() {
pthread_t thread1, thread2;
initlock(&lock);
pthread_create(&thread1, NULL, worker, "Thread1");
pthread_create(&thread2, NULL, worker, "Thread2");
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("Counter value: %d\n", counter);
return 0;
}