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
19
votes
3 answers

Explain Michael & Scott lock-free queue alorigthm

I am studying Michael & Scott's lock-free queue algorithm and trying to implemented it in C++. But I produced a race in my code and think there may be a race in the algorithm. I read the paper here: Simple, Fast, and Practical Non-Blocking and…
Eddie Deng
  • 1,399
  • 1
  • 18
  • 30
19
votes
3 answers

Force order of execution of C statements?

I have a problem with the MS C compiler reordering certain statements, critical in a multithreading context, at high levels of optimization. I want to know how to force ordering in specific places while still using high levels of optimization. (At…
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
18
votes
1 answer

Acquire/release semantics with non-temporal stores on x64

I have something like: if (f = acquire_load() == ) { ... use Foo } and: auto f = new Foo(); release_store(f) You could easily imagine an implementation of acquire_load and release_store that uses atomic with load(memory_order_acquire) and…
Eloff
  • 20,828
  • 17
  • 83
  • 112
18
votes
4 answers

Lock Free Queue -- Single Producer, Multiple Consumers

I am looking for a method to implement lock-free queue data structure that supports single producer, and multiple consumers. I have looked at the classic method by Maged Michael and Michael Scott (1996) but their version uses linked lists. I would…
Shirish
  • 181
  • 1
  • 1
  • 4
18
votes
8 answers

Memory barriers in userspace? (Linux, x86-64)

It is easy to set memory barriers on the kernel side: the macros mb, wmb, rmb, etc. are always in place thanks to the Linux kernel headers. How to accomplish this on the user side?
anon
16
votes
2 answers

Is there a lock-free vector implementation?

First result in Google for "lock free vector" is a research paper cowritten by Damian Dechev, Peter Pirkelbauer and Bjarne Stroustrup describing a theoretical lock-free vector. Has this, or any other lock-free vector, been implemented?
qdii
  • 12,505
  • 10
  • 59
  • 116
16
votes
6 answers

How to achieve lock-free, but blocking behavior?

I'm implementing a lock-free single producer single consumer queue for an intensive network application. I have a bunch of worker threads receiving work in their own separate queues, which they then dequeue and process. Removing the locks from these…
haste
  • 1,441
  • 1
  • 10
  • 21
16
votes
4 answers

Lock-Free Multiple Producer/Consumer Queue in C++11

I'm trying to implement a lock free multiple producer, multiple consumer queue in C++11. I'm doing this as a learning exercise, so I'm well aware that I could just use an existing open source implementation, but I'd really like to find out why my…
Joe
  • 726
  • 1
  • 8
  • 18
16
votes
1 answer

Trying to write a lock-free singly linked list, trouble with the removal

I'm trying to write a lock free singly linked list. Eventual consistency is not a problem (someone traversing a list which might contain incorrect items). I think that I got the add item right (looping and Interlocked.CompareExchange). But I can't…
jgauffin
  • 99,844
  • 45
  • 235
  • 372
16
votes
1 answer

Memory barrier use in lock-free queues

I recently read Paul McKenney's 2010 white paper, "Memory Barriers: a Hardware View for Software Hackers". I would very much appreciate some feedback / comments / guidance with regard to a small sections of C code, given below, which implement the…
user82238
15
votes
1 answer

What is the difference between lock-free and obstruction-free?

I am reading up on Transactional Memory (TM), and one of the papers I'm reading says[1]: Indeed, it was two nonblocking algorithms, the obstruction-free DSTM and lock-free FSTM that reinvigorated STM research in the past decade. I was under the…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
14
votes
5 answers

What is the reason why high level abstractions that use lock free programming deep down aren't popular?

From what I gathered on the lock free programming, it is incredibly hard to do right... and I agree. Just thinking about some problems makes my head hurt. But what I wonder is, why isn't there a widespread use of high-level wrappers around (e.g.…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
14
votes
3 answers

Lock free & Thread-Safe IList for .NET

Is there a lock-free & thread-safe data structure that implements IList? Naturally by lock-free I mean an implementation that makes no use of locking primitives in .NET but rather uses interlocked operations / atomic operations to achieve thread…
damageboy
  • 2,097
  • 19
  • 34
14
votes
1 answer

Understanding CLR 2.0 Memory Model

Joe Duffy, gives 6 rules that describe the CLR 2.0+ memory model (it's actual implementation, not any ECMA standard) I'm writing down my attempt at figuring this out, mostly as a way of rubber ducking, but if I make a mistake in my logic, at least…
Eloff
  • 20,828
  • 17
  • 83
  • 112
14
votes
1 answer

Why is a store-load barrier considered expensive?

Most CPU architectures will re-order stores-load operations, but my question is why? My interpretation of a store-load barrier would look like this: x = 50; store_load_barrier; y = z; Furthermore, I don't see how this barrier would be have much…
1 2
3
43 44