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
31
votes
6 answers

Lock-free Progress Guarantees in a circular buffer queue

Anecdotally, I've found that a lot of programmers mistakenly believe that "lock-free" simply means "concurrent programming without mutexes". Usually, there's also a correlated misunderstanding that the purpose of writing lock-free code is for…
Siler
  • 8,976
  • 11
  • 64
  • 124
31
votes
3 answers

Memory fences: acquire/load and release/store

My understanding of std::memory_order_acquire and std::memory_order_release is as follows: Acquire means that no memory accesses which appear after the acquire fence can be reordered to before the fence. Release means that no memory accesses which…
Siler
  • 8,976
  • 11
  • 64
  • 124
31
votes
9 answers

Portable Compare And Swap (atomic operations) C/C++ library?

Is there any small library, that wrapps various processors' CAS-like operations into macros or functions, that are portable across multiple compilers? PS. The atomic.hpp library is inside boost::interprocess::detail namespace. The author refuses to…
anon
30
votes
4 answers

Does a multiple producer single consumer lock-free queue exist for c++?

The more I read the more confused I become... I would have thought it trivial to find a formally correct MPSC queue implemented in C++. Every time I find another stab at it, further research seems to suggest there are issues such as ABA or other…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
29
votes
6 answers

When are lock free data structures less performant than mutual exclusion (mutexes)?

I read somewhere (can't find the page anymore) that lock free data structures are more efficient "for certain workloads" which seems to imply that sometimes they're actually slower or the gain from them can be zero in some situations. Taking the…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
28
votes
5 answers

How can I verify lock-free algorithms?

In theory, it should be possible to at least brute force a verification of a lock-free algorithm (there are only so many combinations of function calls intersecting). Are there any tools or formal reasoning processes available to actually prove that…
Grant Peters
  • 7,691
  • 3
  • 45
  • 57
27
votes
5 answers

Lock-free and wait-free thread-safe lazy initialization

To perform lock-free and wait-free lazy initialization I do the following: private AtomicReference instance = new AtomicReference<>(null); public Foo getInstance() { Foo foo = instance.get(); if (foo == null) { foo = new Foo(); …
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
24
votes
0 answers

WRITE_ONCE and READ_ONCE in linux kernel

Can somebody explain the usage of WRITE_ONCE and READ_ONCE? And internally WRITE_ONCE uses a volatile qualifier. Why? How does WRITE_ONCE and READ_ONCE solve cache coherency problem? Difference between *(volatile __u8_alias_t *) p and (volatile …
Sai
  • 351
  • 2
  • 5
23
votes
8 answers

How to guarantee 64-bit writes are atomic?

When can 64-bit writes be guaranteed to be atomic, when programming in C on an Intel x86-based platform (in particular, an Intel-based Mac running MacOSX 10.4 using the Intel compiler)? For example: unsigned long long int y; y =…
vok
23
votes
0 answers

`is_always_lock_free` gives `true` but `is_lock_free()` gives `false` on macOS, why?

I'm experimenting with C++ atomic's std::atomic::is_always_lock_free and std::atomic::is_lock_free. I wrote a simple struct A and want to know if the atomic version of A is lock-free: #include #include using namespace…
Shuai
  • 962
  • 1
  • 10
  • 21
23
votes
6 answers

Is it possible to implement lock free map in C++

We are developing a network application based C/S, we find there are too many locks adding to std::map that the performance of server became poor. I wonder if it is possible to implement a lock-free map, if yes, how? Is there any open source code…
Wallace
  • 561
  • 2
  • 21
  • 54
22
votes
5 answers

Thread safe DateTime update using Interlocked.*

Can I use an Interlocked.* synchronization method to update a DateTime variable? I wish to maintain a last-touch time stamp in memory. Multiple http threads will update the last touch DateTime variable. I appreciate that DateTime variables are value…
camelCase
  • 223
  • 1
  • 2
  • 4
21
votes
2 answers

How to implement lock-free skip list

I need to implement a lock-free skip list. I tried to look for papers. Unfortunatly all I found was lock-free single linked lists (in many flavors). However how to implement lock-free skip list?
Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61
21
votes
4 answers

Do atomic operations become slower as more CPUs are added?

x86 and other architectures provide special atomic instructions (lock, cmpxchg, etc.) that allow you to write 'lock free' data structures. But as more and more cores are added, it seems as though the work these instructions will actually have to do…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
20
votes
3 answers

Atomic shared_ptr for lock-free singly linked list

I'm wondering if it is possible to create a lock-free, thread-safe shared pointer for any of the "common" architectures, like x64 or ARMv7 / ARMv8. In a talk about lock-free programming at cppcon2014, Herb Sutter presented a (partial) implementation…
MikeMB
  • 20,029
  • 9
  • 57
  • 102
1
2
3
43 44