Lock Based Concurrent Data Structures

Posted by Beetle B. on Mon 22 June 2020

Concurrent Counters

What if you have a shared counter, and you want several threads to update it?

Simply locking before update will really slow things down.

Another approach is to have each thread have its own counter, and every once in a while, the thread will acquire the lock, and “transfer” its value to the global counter (resetting the local counter). This scales better if the update doesn’t happen too often, but will always be inaccurate. Concurrent Linked Lists A simple approach to adding a node to a shared linked list is to lock the whole list, add the node, and unlock. This doesn’t scale too well. Another approach is to have a lock per node. It turns out this has so much overhead (done for every node access!) that it is an inferior solution. Concurrent Queues You can do this by having separate locks for the head and tail of the list. Concurrent Hash Table Simply make each “bucket” a concurrent linked list! This has good performance!

tags : concurrency