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
-1
votes
2 answers

Reliable way to ensure a Go channel does not block

I'm looking for a reliable to way to make sure an empty channel in Go does not block my execution. I have to iterate through a number of channels in a particular order (kind of priorities), and once I find one with items in it, read one. Currently I…
RobinUS2
  • 955
  • 6
  • 17
-1
votes
3 answers

How to (unit) test if a function is lock free ?

I would like to add several unit tests to my code, also as I load plug ins I don't always have access to the code I'm running. The test I would really like to check is if the function I'm calling is lock free ? Is there any hook, or way to test if…
dzada
  • 5,344
  • 5
  • 29
  • 37
-2
votes
1 answer

Issues with Lock-Free Dynamically Resizing Array Implementation

I've been working on implementing a lock-free array following the instructions from this paper. The goal is to create a thread-safe, dynamically resizing array using lock-free operations. The implementation appears to work fine for small loads.…
-2
votes
1 answer

lock free algorithm version for for below algorithm

i am writing a network controlling feature so the algorithm is read the current transfer rate if it is less than the required transfer rate then proceed else sleep for some x seconds and go to step 1: x is calculated based required transfer rate…
vijay
  • 1
-2
votes
1 answer

Can two boost lockfree queues cause deadlock

My overall design is to have multiple producer threads (>2) to generate results into two atomic processing_done=false; // when all producers finished // will set to true boost::lockfree::queue> Q1; …
Kemin Zhou
  • 6,264
  • 2
  • 48
  • 56
-2
votes
1 answer

Lock free single producer/single consumer circular buffer - Can CPU speculation break the memory barrier logic?

I have been looking at a lock free single producer/single consumer circular buffer when I thought about speculative execution and its effect on the simple code. With this implementation, there is only a unique thread which can call the push()…
Qzaac
  • 159
  • 1
  • 8
-2
votes
2 answers

I heard that INC instruction is not atomic. Then, how CAS instruction can be atomic?

CAS is very primitive lock free technique, and I know that it is atomic. Also, it is much more complex operation than INC. It should compare value and if value is not changed, CAS sets the new value while guaranteeing that other thread does not…
Hozard
  • 193
  • 2
  • 9
-2
votes
1 answer

What're the pros and cons of lock-free programming and lock-based programming?

I've been using C++11 for several months, I've been enjoying most components of the C++11 standard library except those of the atomic-operations-related. In my opinion, lock-free programming is too complicated to get it right, while lock-based…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
1 2 3
43
44