| | theLock has two states: locked or
unlocked. |
| | If a thread tries to lock theLock, and it’s unlocked,
then it becomes locked, and that thread continues executing normally. |
| | If a thread tries to lock theLock, but it’s already
locked, then that thread is suspended, and that thread is added to some sort of list of threads that are waiting on this
particular NSLock object. |
| | If a thread tries to unlock theLock, but it’s already
unlocked, undesirable things may happen. (If you used lock and unlock properly, as in the above example, this
situation will never occur.) |
| | If a thread tries to unlock theLock, and it’s locked,
and its list of waiting threads is empty, then theLock will become unlocked, and the thread that unlocked it will continue
executing. |
| | If a thread tries to unlock theLock, and it’s locked,
and its list of waiting threads is not empty, then theLock will stay locked, but one of the threads in its list
(presumably the one that has been waiting longest) will be removed from the list and reactivated for normal
execution. (Also, the thread that just tried to unlock theLock will continue executing.) |
While a thread is suspended, waiting on an NSLock, it isn’t chewing up any processor time or occupying a processor core
— it’s truly suspended, to be reactivated later by the NSLock object that it tried to lock.