Previous | Next --- Slide 39 of 66
Back to Lecture Thumbnails
nickbowman

The "hand over hand" locking policy refers to the importance of grabbing the lock for the next element in the sequence before releasing the lock that you are currently holding on to. This is important because it means that there is never a situation where we open ourselves up to the potential of holding zero locks, which degrades back to the unsynchronized/data race case from before. This would like the real-life situation where you don't have your hands on any bars, at which point you would fall.

ufxela

To elaborate on @nickbowmann's point about why we can never hold zero locks, here's an example:

Say thread 1 is trying to insert a node of val 4.

1 -> 2 -> 3 -> 5

Thread 1 is at the point where cur has val 2, prev has val 1. Then it lets go of all locks. Then, thread 1 get thrown off the processor and thread 2 pops in and completes a deletion of the node with val 3. Now thread 1 starts running again and tries to execute the line cur = cur->next. But thread 2 just deleted cur. We crash.

The intuition behind this is if you aren't holding at least one lock on the list, then you have no grasp on the list--anything could happen to it while you're not "looking".

Jonathan

One interesting thought is that this "hand over hand" policy can lead to deadlock in the doubly linked list case, since traversals from opposite directions would deadlock when they meet. It seems like hand over hand locking must carefully restrict what order locks are acquired in, not only to prevent falling off (desync), but also to prevent a head on collision (deadlock).

jchen

Would hand over hand locking end up significantly decreasing parallelism? Since it seems like if one thread tries to insert at the beginning of the list, then no other thread can try to insert or delete at a later part of the list, even if it's completely independent, until the first thread relinquishes its lock. Back to the monkey bars analogy, if someone is hanging ahead of you on the monkey bars, you can't skip over them to progress even your destination bars are different from theirs, right?

Please log in to leave a comment.