Concurrent Linked Lists
Explore how to manage concurrency in linked lists by using locks effectively during insert operations while handling memory allocation failures. Understand code structuring to minimize locking errors and investigate hand-over-hand locking to improve concurrency, along with its practical performance trade-offs.
We'll cover the following...
You will now examine a more complicated structure, the linked list. Let’s start with a basic approach once again. For simplicity, we’ll omit some of the obvious routines that such a list would have and just focus on concurrent insert; we’ll leave it to the reader to think about lookup, delete, and so forth. The code excerpt below shows the code for this rudimentary data structure.
As you can see in the code, the code simply acquires a lock in the insert routine upon entry and releases it upon exit. One small tricky issue arises if malloc() happens to fail (a rare case); in this case, the code must also release the lock before failing the insert.
This kind of exceptional control flow is quite error-prone; a recent study of Linux kernel patches found that a huge fraction of bugs (nearly 40%) are found on such rarely-taken code paths (indeed, this observation sparked some of ...