Similar questions 1 more →
Synchronization & Concurrency




Answer & solution
Answer: enter_cs: while (TEST_AND_SET(&mutex)) { } ; leave_cs: mutex=0; (no official key was published)
Use the shared lock variable mutex, initially zero:
void enter_cs(void) { while (TEST_AND_SET(&mutex)) { } }
void leave_cs(void) { mutex = 0; }
TEST_AND_SET atomically returns the old value and writes one. Exactly one contender observing the unlocked zero receives zero and leaves the loop; later contenders receive one and keep spinning. Releasing the lock restores zero for a future acquisition. This is pseudocode for the given atomic primitive and a shared lock, not a claim that an ordinary non-atomic C integer and a handwritten function implement modern concurrent-C synchronization. The source's hyphenated function names are rendered with underscores to make the names valid C identifiers.