/* * Tentativa inútil de proteger a região crítica. */ #include #include #include #include volatile int s = 0; /* Variável compartilhada */ volatile int lock = 0; void* f_thread_0(void *v) { while (lock == 1); /* Espera a "obtenção" do lock */ lock = 1; s = 0; sleep(1); printf("Thread 0, s = %d.\n", s); lock = 0; /* Libera o lock */ return NULL; } void* f_thread_1(void *v) { while (lock == 1); /* Espera a "obtenção" do lock */ lock = 1; s = 1; printf("Thread 1, s = %d.\n", s); lock = 0; /* Libera o lock */ return NULL; } int main() { pthread_t thr0, thr1; pthread_create(&thr0, NULL, f_thread_0, NULL); pthread_create(&thr1, NULL, f_thread_1, NULL); pthread_join(thr0, NULL); pthread_join(thr1, NULL); return 0; }