Questions tagged [lockless]

Lockless operations guarantee simultaneous access to data structures without the usage of conventional locks which are generally slow operations like critical sections, mutexes etc. Lockless operations can be achieved with atomic operations for example.

Lockless operations guarantee simultaneous access to data structures without the usage of conventional locks which are generally slow operations like critical sections, mutexes etc. Lockless operations can be achieved with atomic operations() for example.

82 questions
6
votes
1 answer

What is the fastest race free method for polling a lockless queue?

Say we have a single-producer-thread single-consumer-thread lockless queue, and that the producer may go long periods without producing any data. It would be beneficial to let the consumer thread sleep when there is nothing in the queue (for the…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
5
votes
1 answer

Is using std::atomic_thread_fence right before an atomic load/store with the same order always redundant?

Given: std::atomic b; void f() { std::atomic_thread_fence(std::memory_order::memory_order_acquire); uint64_t a = b.load(std::memory_order::memory_order_acquire); // code using a... } Can removing the call to…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165
5
votes
1 answer

Cache line padding for variables that are a multiple of cache line size

I am creating a very fast multi-threaded discrete event simulation framework. The core of the framework uses atomics and lockless programming techniques to achieve very fast execution across many threads. This requires me to align some variables to…
nic
  • 1,511
  • 2
  • 14
  • 27
5
votes
2 answers

Simple lockless stopwatch

According to MSDN, the Stopwatch class instance methods aren't safe for multithreaded access. This can also be confirmed by inspecting individual methods. However, since I only need simple "time elapsed" timers at several places in my code, I was…
Lou
  • 4,244
  • 3
  • 33
  • 72
5
votes
1 answer

Lockless queue implementation ends up having a loop under stress

I have lockless queues written in C in form of a linked list that contains requests from several threads posted to and handled in a single thread. After a few hours of stress I end up having the last request's next pointer pointing to itself, which…
Fozi
  • 4,973
  • 1
  • 32
  • 56
5
votes
0 answers

How do emulators handle translating memory barriers (implicit and explicit)?

Assuming the source and target architectures are different, how do emulators efficiently translate memory barriers? I know that in general modern emulators will employ a JIT to translate from the source ISA to the target ISA, but knowing which code…
5
votes
3 answers

Thread-safe generic field

I have a generic field and a property that encapsulates it: T item; public T Item { get { return item; } set { item = value; } } The problem is that this property can be written to from one thread and read from multiple threads at the same…
svick
  • 236,525
  • 50
  • 385
  • 514
4
votes
3 answers

How should I register an update in a callback from another thread without using locks?

This is one of those questions that seems to fall into the "naively obvious but probably wrong" category. Certainly, I'm struggling to find a solution that works in all corner cases. It seems like this must be a problem that is encountered all the…
Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
4
votes
3 answers

How can I evaluate performances of a lockless queue?

I have implemented a lockless queue using the hazard pointer methodology explained in http://www.research.ibm.com/people/m/michael/ieeetpds-2004.pdf using GCC CAS instructions for the implementation and pthread local storage for thread local…
Raffo
  • 1,642
  • 6
  • 24
  • 41
4
votes
2 answers

Are X86 atomic RMW instructions wait free

On x86, atomic RMW instructions like lock add dword [rdi], 1 are implemented using cache locking on modern CPUs. So a cache line is locked for duration of the instruction. This is done by getting the line EXCLUSIVE/MODIFIED state when value is read…
pveentjer
  • 10,545
  • 3
  • 23
  • 40
4
votes
1 answer

Is Ruby Array#[]= threadsafe for a preallocated array? Can this be made lockless?

I've written some code in ruby to process items in an array via a threadpool. In the process, I've preallocated a results array which is the same size as the passed-in array. Within the threadpool, I'm assigning items in the preallocated array, but…
Michael Bishop
  • 4,240
  • 3
  • 37
  • 41
4
votes
1 answer

Atomic thread counter

I'm experimenting with the C++11 atomic primitives to implement an atomic "thread counter" of sorts. Basically, I have a single critical section of code. Within this code block, any thread is free to READ from memory. However, sometimes, I want…
Siler
  • 8,976
  • 11
  • 64
  • 124
4
votes
7 answers

Lock-free C++ data structures, impossible?

I really dont understand how you can make some data structures lock-free. For example, if you have a linked list then either you surround the operations with mutexes, OR you can end up with a race condition if another thread executes whilst you are…
user997112
  • 29,025
  • 43
  • 182
  • 361
4
votes
3 answers

Is lockless hashing without std::atomics guaranteed to be thread-safe in C++11?

Consider the following attempt at a lockless hashtable for multithreaded search algorithms (inspired by this paper) struct Data { uint64_t key; uint64_t value; }; struct HashEntry { uint64_t key_xor_value; uint64_t value; }; void…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
3
votes
1 answer

Does memory_order_relaxed respect data dependencies within the same thread?

Given: std::atomic x; uint64_t f() { x.store(20, std::memory_order::memory_order_relaxed); x.store(10, std::memory_order::memory_order_relaxed); return x.load(std::memory_order::memory_order_relaxed); } Is it ever possible…
Joseph Garvin
  • 20,727
  • 18
  • 94
  • 165