Questions tagged [lock-free]

An umbrella term for methods and algorithms to synchronize multithreaded environments or other forms of distributed system without using locks.

An umbrella term for methods and algorithms to synchronize multithreaded environments or other forms of distributed system without using locks.

The necessity for lock-free software architectures arises mainly from performance and scalability issues with using locks for synchronization: Using a lock requires a writer to exclude all readers, but very fine-grained locking (per hash bucket for example) takes extra storage and more lock/unlock overhead.

  • Jeff Preshing's An Introduction to Lock-Free Programming is a good starting point, especially in / using (see that tag wiki for more links).

  • A practical example is a lock-free hash table for multi-threaded C++ programs that run on SMP hardware. It only requires the widely supported atomic operations: load, store, or read-modify-write (e.g. compare-and-swap) of a single location.

  • Can num++ be atomic for 'int num'?: Why read-modify-write operations are not atomic even if they compile to a single asm instruction, and how this all works under the hood in assembly.


Hardware Transactional Memory is a more powerful system architectures for lock-free programming, allowing a group of multiple operations (even on different addresses) to be grouped into a single atomic operation. (The software variant STM might use locks for synchronization). HTM is supported on recent (2015/2016) x86 hardware.

See also the Communicating sequential processes and Compare-and-swap instruction.

653 questions
364
votes
11 answers

When should the volatile keyword be used in C#?

Can anyone provide a good explanation of the volatile keyword in C#? Which problems does it solve and which it doesn't? In which cases will it save me the use of locking?
Doron Yaacoby
  • 9,412
  • 8
  • 48
  • 59
107
votes
3 answers

atomic operation cost

What is the cost of the atomic operation (any of compare-and-swap or atomic add/decrement)? How much cycles does it consume? Will it pause other processors on SMP or NUMA, or will it block memory accesses? Will it flush reorder buffer in…
osgx
  • 90,338
  • 53
  • 357
  • 513
92
votes
15 answers

Is there a production ready lock-free queue or hash implementation in C++

I ve been googling quite a bit for a lock-free queue in C++. I found some code and some trials - but nothing that i was able to compile. A lock-free hash would also be welcome. SUMMARY: So far i have no positive answer. There is no "production…
RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92
91
votes
6 answers

Lock-free multi-threading is for real threading experts

I was reading through an answer that Jon Skeet gave to a question and in it he mentioned this: As far as I'm concerned, lock-free multi-threading is for real threading experts, of which I'm not one. Its not the first time that I have heard this,…
vdhant
  • 2,218
  • 4
  • 24
  • 28
79
votes
18 answers

Circular lock-free buffer

I'm in the process of designing a system which connects to one or more stream of data feeds and do some analysis on the data than trigger events based on the result. In a typical multi-threaded producer/consumer setup, I will have multiple producer…
Shing Yip
  • 1,596
  • 1
  • 12
  • 11
67
votes
4 answers

How are atomic operations implemented at a hardware level?

I get that at the assembly language level instruction set architectures provide compare and swap and similar operations. However, I don't understand how the chip is able to provide these guarantees. As I imagine it, the execution of the instruction…
Alexander Duchene
  • 1,107
  • 1
  • 11
  • 14
62
votes
2 answers

Using Boost.Lockfree queue is slower than using mutexes

Until now I was using std::queue in my project. I measured the average time which a specific operation on this queue requires. The times were measured on 2 machines: My local Ubuntu VM and a remote server. Using std::queue, the average was almost…
Bobface
  • 2,782
  • 4
  • 24
  • 61
56
votes
6 answers

Reading an int that's updated by Interlocked on other threads

(This is a repeat of: How to correctly read an Interlocked.Increment'ed int field? but, after reading the answers and comments, I'm still not sure of the right answer.) There's some code that I don't own and can't change to use locks that increments…
Michael Covelli
  • 2,113
  • 4
  • 27
  • 41
55
votes
11 answers

Do lock-free algorithms really perform better than their lock-full counterparts?

Raymond Chen has been doing a huge series on lockfree algorithms. Beyond the simple cases of the InterlockedXxx functions, it seems like the prevailing pattern with all of these is that they implement their own locks. Sure, there are not processor…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
49
votes
3 answers

Lock-free swap of two unique_ptr

Swapping two unique_ptrs is not guaranteed to be threadsafe. std::unique_ptr a, b; std::swap(a, b); // not threadsafe Since I need atomic pointer swaps and since I like the ownership handling of unique_ptr, is there a simple way to combine them…
user1181282
  • 766
  • 1
  • 7
  • 11
45
votes
7 answers

Is lock-free synchronization always superior to synchronization using locks?

In C++, there is one atomic type std::atomic. This atomic type may be lock-free or maybe not depending on the type T and on the current platform. If a lock-free implementation for a type is available in a platform for a type T, then most…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
42
votes
5 answers

Can you avoid locking by guaranteeing that multiple threads won't access the same memory?

Say I have a large array and I want to process the contents with multiple threads. If I delegate each thread to a specific section, guaranteeing no overlap, does that eliminate any need for locking, assuming the threads don't access any other…
Elden Abob
  • 603
  • 4
  • 13
40
votes
3 answers

Is x86 CMPXCHG atomic, if so why does it need LOCK?

The Intel documentation says This instruction can be used with a LOCK prefix to allow the instruction to be executed atomically. My question is Can CMPXCHG operate with memory address? From the document it seems not but can anyone confirm that…
Alex Suo
  • 2,977
  • 1
  • 14
  • 22
36
votes
21 answers

How can I write a lock free structure?

In my multithreaded application and I see heavy lock contention in it, preventing good scalability across multiple cores. I have decided to use lock free programming to solve this. How can I write a lock free structure?
Suma
  • 33,181
  • 16
  • 123
  • 191
31
votes
4 answers

Lock free stack and queue in C#

Does anyone know if there are any lock-free container libraries available for .NET ? Preferably something that is proven to work and faster than the Synchronized wrappers we have in .NET. I have found some articles on the .NET, but none of them…
Radu094
  • 28,068
  • 16
  • 63
  • 80
1
2 3
43 44