Questions tagged [interlocked]

Provides atomic operations for variables that are shared by multiple threads.

Provides atomic operations for variables that are shared by multiple threads.

References

233 questions
16
votes
3 answers

What's Java's equivalent of .Net's Interlocked class?

How do I modify an int atomically and thread-safely in Java? Atomically increment, test & set, etc...?
ripper234
  • 222,824
  • 274
  • 634
  • 905
15
votes
3 answers

Using Interlocked.CompareExchange with a class

System.Threading.Interlocked.CompareExchange operator provides atomic (thus thread-safe) C# implementation of the Compare-And-Swap operation. For example int i = 5; Interlocked.CompareExchange(ref i, 10, 5); After this command, the int i would have…
Bhargav Mangipudi
  • 1,215
  • 2
  • 10
  • 13
15
votes
3 answers

Memory barrier vs Interlocked impact on memory caches coherency timing

Simplified question: Is there a difference in timing of memory caches coherency (or "flushing") caused by Interlocked operations compared to Memory barriers? Let's consider in C# - any Interlocked operations vs Thread.MemoryBarrier(). I believe…
Jan
  • 1,905
  • 17
  • 41
13
votes
2 answers

Difference between Threading.Volatile.Read(Int64) and Threading.Interlocked.Read(Int64)?

What is the difference, if any, of the Read(Int64) method of the .NET system classes System.Threading.Volatile and System.Threading.Interlocked? Specifically, what are their respective guarantees / behaviour with regard to (a) atomicity and (b)…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
13
votes
2 answers

Interlocked used to increment/mimick a boolean, is this safe?

I'm just wondering whether this code that a fellow developer (who has since left) is OK, I think he wanted to avoid putting a lock. Is there a performance difference between this and just using a straight forward lock? private long…
Sarah Fordington
  • 439
  • 1
  • 6
  • 12
12
votes
1 answer

Can Interlocked.Increment overflow cause .NET runtime corruption?

The MSDN documentation for Interlocked.Increment states: This method handles an overflow condition by wrapping: if location = Int32.MaxValue, location + 1 = Int32.MinValue. No exception is thrown. What does “location + 1” mean in this context? If…
Douglas
  • 53,759
  • 13
  • 140
  • 188
11
votes
5 answers

Does Interlocked provide visibility in all threads?

Suppose I have a variable "counter", and there are several threads accessing and setting the value of "counter" by using Interlocked, i.e.: int value = Interlocked.Increment(ref counter); and int value = Interlocked.Decrement(ref counter); Can I…
11
votes
7 answers

Is Interlocked.CompareExchange really faster than a simple lock?

I came across a ConcurrentDictionary implementation for .NET 3.5 (I'm so sorry I could find the link right now) that uses this approach for locking: var current = Thread.CurrentThread.ManagedThreadId; while (Interlocked.CompareExchange(ref owner,…
Andre Pena
  • 56,650
  • 48
  • 196
  • 243
10
votes
9 answers

Is a lock (wait) free doubly linked list possible?

Asking this question with C# tag, but if it is possible, it should be possible in any language. Is it possible to implement a doubly linked list using Interlocked operations to provide no-wait locking? I would want to insert, add and remove, and…
esac
  • 24,099
  • 38
  • 122
  • 179
10
votes
2 answers

How to use interlocked operations against memory-mapped files in .Net

Is there any way to use the Interlocked.CompareExchange(); and Interlocked.Increment(); methods against values stored in a memory-mapped file? I'd like to implement a multi-threaded service that will store its data in a memory-mapped file, but since…
Mike Schenk
  • 1,512
  • 9
  • 21
10
votes
2 answers

Should interlocked implementations based on CompareExchange use SpinWait?

Below is an implementation of an interlocked method based on Interlocked.CompareExchange. Is it advisable for this code to use a SpinWait spin before reiterating? public static bool AddIfLessThan(ref int location, int value, int comparison) { …
Timo
  • 7,992
  • 4
  • 49
  • 67
10
votes
2 answers

C# Code optimization causes problems with Interlocked.Exchange()

I have a frustrating problem with a bit of code and don't know why this problem occurs. // // .NET FRAMEWORK v4.6.2 Console App static void Main( string[] args ) { var list = new List{ "aa", "bbb", "cccccc", "dddddddd",…
Ronin
  • 179
  • 11
10
votes
1 answer

Atomic load in C with MSVC

TL;DR: I need the Microsoft C (not C++) equivalent of C11's atomic_load. Anyone know what the right function is? I have some pretty standard code which uses atomics. Something like do { bar = atomic_load(&foo); baz = some_stuff(bar); } while…
nemequ
  • 16,623
  • 1
  • 43
  • 62
10
votes
1 answer

Why does Interlocked.Increment give an incorrect result in a Parallel.ForEach loop?

I have a migration job and I need to validate the target data when done. To notify the admin of the success/failure of validations, I use a counter to compare the number of rows from table Foo in Database1 to the number of rows from table Foo in…
Benjamin Beaulieu
  • 995
  • 2
  • 10
  • 26
10
votes
1 answer

Why everyone states that SpinLock is faster?

I have read a lot of docs and articles and posts all over the internet. Almost everyone and everywhere commits that SpinLock is faster for a short running pieces of code, but I made a test, and it appears to me that simple Monitor.Enter works faster…
Rauf
  • 312
  • 3
  • 16
1
2
3
15 16