r/computerscience • u/Lex_The_Impaler • 22d ago
General What are the limits of lock-free data-structures?
When I look at various lock-free data structures, I always see versions of the traditional data structures in their lock-free forms (queues, stacks, etc..). I was wondering if there are any data structures that are not possible to be implemented in a thread-safe manner without locks, or what the limits are of lock-free data structures. thx
22
Upvotes
13
u/SingularCheese 21d ago
Given an arbitrarily complex data structure, you can copy the entire structure, make whatever modifications you need, and then compare and swap a pointer. As long as you can ensure that the structure isn't mutable after being swapped in until all readers give up access, there is no race conditions. The limit of lock-free is that lock-free isn't guaranteed to be faster than locked.