#include #include #include typedef struct { atomic_int locked; } Spinlock; void spinlock_acquire(Spinlock *lock) { while (atomic_exchange(&lock->locked, 1)) { // Keep spinning until the lock is free } } void spinlock_release(Spinlock *lock) { atomic_store(&lock->locked, 0); } void *worker(void *arg) { Spinlock *lock = (Spinlock *)arg; spinlock_acquire(lock); printf("Thread %ld acquired the lock\n", pthread_self()); spinlock_release(lock); return NULL; } int main() { pthread_t t1, t2; Spinlock lock = {0}; pthread_create(&t1, NULL, worker, &lock); pthread_create(&t2, NULL, worker, &lock); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }