Created
March 23, 2018 18:36
-
-
Save exhesham/00fe10fd9a6867b4001d718fd4b2f61f to your computer and use it in GitHub Desktop.
mutex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function lock(boolean *lock) { | |
while (test_and_set(lock) == 1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* set the lock to true and send its previous value. lock=true means it is locked. | |
Notice that the test_and_lock always locks the lock (set lock == true) and returns the previous value before the locking*/ | |
boolean_ref test_and_set(boolean_ref lock) { | |
boolean initial = lock; | |
lock = true; | |
return initial; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment