Linked Questions
13 questions linked to/from Does Interlocked.CompareExchange use a memory barrier?
31 votes
2 answers
7k views
Difference between Interlocked.Exchange and Volatile.Write?
What is the difference between Interlocked.Exchange and Volatile.Write? Both methods update value of some variable. Can someone summarize when to use each of them? Interlocked.Exchange Volatile.Write ...
10 votes
7 answers
717 views
lock free containers and visibility
I've seen some lock free implementations of stack... My question is regarding the visibility, not atomicity. For example do elements(not pointers) of lock free stack must be at most 64bit? I think so, ...
16 votes
2 answers
2k views
For purposes of ordering, is atomic read-modify-write one operation or two?
Consider an atomic read-modify-write operation such as x.exchange(..., std::memory_order_acq_rel). For purposes of ordering with respect to loads and stores to other objects, is this treated as: a ...
7 votes
2 answers
792 views
Using a F# event and asynchronous in multi-threaded code
EDIT/Notice: Event is now thread-safe in current F# implementation. I'm working a lot with asynchronous workflows and agents in F#. While I was going a little bit deeper into events I noticed that ...
1 vote
4 answers
365 views
Compare and exchange as a nonatomic operation
I got a book about programming in C#. There is an example. Let me copy and paste the context. ...Introduced the usage of Interlocked(omitted here). You can also use the CompareExchange method. ...
7 votes
1 answer
2k views
Compare and Swap on x86 - why is it a full barrier?
As per this question's answer, it seems that LOCK CMPXCHG on x86 actually causes a full barrier. Presumably, this is what Unsafe.compareAndSwapInt() generates under the hood as well. I am struggling ...
4 votes
2 answers
411 views
Do I need MemoryBarrier with ReaderWriterLockSlim?
It looks like the mono implementation has no MemoryBarrier calls inside the ReaderWriterLockSlim methods. So when I make any changes inside a write lock, I can receive old cached values in another ...
9 votes
2 answers
612 views
Threading & implicit memory barriers
Trying to understand .net's memory model when it comes to threading. This question is strictly theoretical and I know it can be resolved in other ways such as using a lock or marking _task as volatile....
2 votes
1 answer
412 views
Any way to limit program to one CPU without caring which one?
I have a program which is occasionally malfunctioning and I'm wondering whether the problems might be related to different threads running on different cores handling reads and writes in a different ...
-1 votes
2 answers
165 views
Data coherency on multi-threaded environment without concurrent data access
Does memory barrier ensure data coherence across threads when there is no locking and no concurrent data access except from the parent thread ? Here is my scenario : the Main thread launch several ...
2 votes
2 answers
171 views
Is it correct to perform regular reads on a field lazily-initialized by Interlocked.CompareExchange?
Suppose you have a property public Foo Bar { get; } that you want to lazily initialize. One such approach might be to use the Interlocked class, which guarantees atomicity for certain sequences of ...
-1 votes
1 answer
188 views
Lockless multithreading of an integer
Given a scenario where there's a function that should only be executed by one thread at any given time, and the rest just return (since a specific state is already being worked on), what's the best ...
0 votes
0 answers
133 views
Reading value before InterLocked.CompareExchange [duplicate]
I see following code in C# usage of InterLocked: class MyObj { // some class code .. int myVal public void foo() { int currVal = 0, newVal = 0; do { ...