#define _GNU_SOURCE #include #include #include #include #include "myfutex.h" #include volatile atomic_t val = {-1}; void lock() { atomic_t c; c.counter = val.counter; while (!atomic_inc_and_test(&val)) { futex_wait(&val.counter, c.counter+1); c.counter = val.counter; } } void unlock() { val.counter = -1; futex_wake(&val.counter, 1); } void* f_thread0(void *v) { printf("Thread 0 antes do lock.\n"); lock(); sleep(3); unlock(); printf("Thread 0 após o lock.\n"); return NULL; } void* f_thread1(void *v) { printf("Thread 1 antes do lock.\n"); lock(); sleep(3); unlock(); printf("Thread 1 após o lock.\n"); return NULL; } int main() { pthread_t thr0, thr1; pthread_create(&thr0, NULL, f_thread0, NULL); pthread_create(&thr1, NULL, f_thread1, NULL); pthread_exit(0); }