Talk:Lock (computer science)
From Wikipedia, the free encyclopedia
[edit] The code example in the article doesn't guarantee mutual exclusion
I removed the following code example from the article as it doesn't solve the atomicity problem:
while (1) { /* loop until lock is obtained */ if (lock == 0) { /* lock not set */ lock = myPID; /* set lock with my process ID */ sleep(1); /* wait for a short time */ if (lock == myPID) break; /* we still have the lock */ } /* we still don't have the lock */ sleep(someTime); /* wait for some period of time */ } //Critical Section
Let's assume Process A and B are executing. A executes, checks the if guard that lock==0, proceeds then does a context switch. B then checks the lock, confirms it's open, proceeds and does a context switch. A then sets lock to its PID, and sleeps. There's no guarantee that B will wake up next and overwrite the lock variable. A wakes up 1s later, checks that it still does have the lock, breaks and enters the critical section. At some point it does a context switch. B then proceeds as normal; it overwrites the lock variable, sleeps, rechecks it has the lock, then enters the critical section as well.
--Chozan 02:23, 12 November 2005 (UTC)
[edit] Lock Modality?
Along with the ideas of granularity, perhaps there could be some discussion of the modality of locking schemes (shared locks, exclusive locks, intent locks) and the obligatory table of the compatibility of different locks. I don't feel competent enough to write this section myself, but if anyone with familiarity in the subject would be interested, perhaps [1], [2], or [3] could serve as useful references?
--Kevinoid 14:32, 28 November 2006 (UTC)