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
0
votes
1 answer

Intel TBB tasks for serving network connections - good model?

I'm developing a backend for a networking product, that serves a dozen of clients (N = 10-100). Each connection requires 2 periodic tasks, the heartbeat, and downloading of telemetry via SSH, each at H Hz. There are also extra events of different…
Tosha
  • 998
  • 1
  • 10
  • 22
0
votes
1 answer

simple thread safe and fast memory pool implementation?

After rethinking the design and with some input from paddy I came up with something like this, but I wonder on the correctness of it, it seems fine when I run it... The idea is that preallocated objects inherit from the following: struct Node { …
Palace Chan
  • 8,845
  • 11
  • 41
  • 93
0
votes
3 answers

Is there a way to implement a lock free static configuration data?

public class MyConfigurationData { public double[] Data1 { get; set; } public double[] Data2 { get; set; } } public class MyClass { private static object SyncObject = new object(); private static MyConfigurationData = null; private…
Imran
  • 694
  • 5
  • 12
0
votes
1 answer

How to use valgrind with lock-free data structures?

I'm experimenting with lock-free structures in the liblfds library (http://www.liblfds.org/) with an eye towards using them in a toolchain that also incorporates valgrind for various error checks, which is causing me some trouble. I built a debug…
BD at Rivenhill
  • 12,395
  • 10
  • 46
  • 49
0
votes
2 answers

Lock free list with sync_val_compare_and_swap

I'm trying to provide a thread safe way to update an object. I've seen a few posts about it, but nothing that answers my question 100%. What I want to do is maintain a pointer called 'm_First' to point at my data. If someone calls get, I deference…
0
votes
2 answers

Single producer single consumer lock free queue c

I am wondering why it would be incorrect to implement this sort of queue the naive way: #include #include #include #include void *print_message_function( void *ptr ); void *reader( void *ptr ); void…
BAR
  • 15,909
  • 27
  • 97
  • 185
0
votes
2 answers

Inconsistent MSDN documentation for InterlockedExchange() type functions/intrinsics?

First, we have InterlockedExchange64(); http://msdn.microsoft.com/en-us/library/windows/desktop/ms683593%28v=vs.85%29.aspx LONGLONG __cdecl InterlockedExchange64( __inout LONGLONG volatile *Target, __in LONGLONG Value ); Second, we have the…
user82238
0
votes
3 answers

How do I implement the ABA solution?

I am trying to implement the Michael-Scott FIFO queue from here. I'm unable to implement their solution for the ABA problem. I get this error. error: incompatible type for argument 1 of '__sync_val_compare_and_swap' For reference, I am using a…
Boilermaker
  • 31
  • 1
  • 3
0
votes
2 answers

Some doubts on lock free programming

Hi Folks, First of all sorry, this is going to be a moderately long post.So, please have the patience to read it through. Here, I am going to put down some concepts which I have learned while skimming through some articles on lock-free…
Arunmu
  • 6,837
  • 1
  • 24
  • 46
-1
votes
1 answer

Interlocked.Exchange influence on following instructions

If thread 1 runs: this.Field.Flag = false; ... var oldValue = Interlocked.Exchange(ref this.Field, newValue); oldValue.Flag = true; and thread 2 sees oldValue.Flag == true, is it guaranteed that it also sees this.Field == newValue even if it…
andresp
  • 1,624
  • 19
  • 31
-1
votes
1 answer

Lock-free fetch_add much slower than mutex?

My test shows that using lock-free atomic fetch_add is much slower than using mutex. I am running 10 parallel threads adding 1 million to the shared counter each. I am using relaxed memory order. Did I do the atomic wrong? output from…
Kenneth
  • 561
  • 1
  • 5
  • 13
-1
votes
1 answer

Interlock compareexchange for collection

I am using a concurrentqueue so multiple users can write their information in. At the background, I am using a timer to save down whatever in the queue to the database on a time basis. My code is as blow: public void WriteInformation(sting msg) { …
Helic
  • 907
  • 1
  • 10
  • 25
-1
votes
1 answer

Impossible constraint with cmpxchg16b in extended assembly

I am trying to write inline assembly with my C code to perform compare and swap operation. My code is: typedef struct node { int data; struct node * next; struct node * backlink; int flag; int mark; } node_lf; typedef struct…
Ritesh
  • 67
  • 8
-1
votes
1 answer

Why does this lock free queue work?

I'm rewriting an old lock free queue implementation, I started by using memory_order_relaxed for everything with the intention of tightening up the memory semantics and adding standalone fences etc later. But strangely, it's working.. I've tried…
Joe
  • 726
  • 1
  • 8
  • 18
-1
votes
1 answer

Ordered forward lock-free list implemented in C

I need lock-free data structure that confirms to certain criteria: fast, low memory consumption, simple to implement, ordered. Have been searching, and found data structures such as single-linked list, doubly-linked list, skip lists, but none of…
Dawid Szymański
  • 775
  • 6
  • 15
1 2 3
43
44