solution
One approach for using compare and swap() for implementing a spinlock is as follows: void lock spinlock(int *lock) { while (compare and swap(lock, 0, 1) != 0) ; /* spin */ } A suggested alternative approach is to use the “compare and compareand-swap†idiom, which checks the status of the lock before invoking the EX-19 Exercises compare and swap() operation. (The rationale behind this approach is to invoke compare and swap()only if the lock is currently available.) This strategy is shown below: void lock spinlock(int *lock) { { while (true) { if (*lock == 0) { /* lock appears to be available */ if (!compare and swap(lock, 0, 1)) break; } } } Does this “compare and compare-and-swap†idiom work appropriately for implementing spinlocks? If so, explain. If not, illustrate how the integrity of the lock is compromised.
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"

