A solution to a real life synchronization problem. The doorknob/sock combo is essentially an unenforced mutex object. If a roommate thread violates the mutex, either (linux dorms) the locking thread receives a SIG_COITUS interruptus or (Win98 dorms) the system crashes with an orgy sharing violation (thx m_turner!).
Both roommates want some private time in the room, but neither must enter while the other one is busy. When one roommate wants exclusive access to the room, they place a sock on the doorknob. Both are instructed not to enter the room if they see a sock in the door, and to wait until later. The sexiled roommate can check the doorknob at regular intervals or perhaps use exponential backoff timing between checks.
In code (for each roommate thread
This is an example of a spinlock using an atomic (uninterruptable) testAndSet() function. It assumes that both roommates are not in the room at the time the algorithm begins. Spinlocks in general are inefficient since the waiting roommate(s) are not doing anything productive while waiting. In a semaphore system, for example, the roommates would have pagers that would allow them to communicate via interrupts.