__monitor int get_lock(void) { if (the_lock == 0) { /* Success, we managed to lock the lock. */ the_lock = 1; return 1; }
else { /* Failure, someone else has locked the lock. */ return 0; } }
/* release_lock -- Unlock the lock. */ __monitor void release_lock(void) { the_lock = 0; }
void my_program(void) { if (get_lock()) { /* ... Do something ... */ /* When done, release the lock. */ release_lock(); } }
|