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

209 questions
725
votes
9 answers

Volatile vs. Interlocked vs. lock

Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, which approach should be used, and why? lock(this.locker)…
core
  • 30,054
  • 41
  • 131
  • 189
46
votes
4 answers

Reading an int that's updated by Interlocked on other threads

(This is a repeat of: How to correctly read an Interlocked.Increment'ed int field? but, after reading the answers and comments, I'm still not sure of the right answer.) There's some code that I don't own and can't change to use locks that increments…
Michael Covelli
  • 1,931
  • 3
  • 25
  • 39
40
votes
6 answers

This is Thread-Safe right?

Just checking... _count is being accessed safely, right? Both methods are accessed by multiple threads. private int _count; public void CheckForWork() { if (_count >= MAXIMUM) return; Interlocked.Increment(ref _count); Task t =…
Andres A.
  • 1,275
  • 2
  • 17
  • 32
37
votes
4 answers

Alignment requirements for atomic x86 instructions vs. MS's InterlockedCompareExchange documentation?

Microsoft offers the InterlockedCompareExchange function for performing atomic compare-and-swap operations. There is also an _InterlockedCompareExchange intrinsic. On x86 these are implemented using the lock cmpxchg instruction. However, reading…
jalf
  • 229,000
  • 47
  • 328
  • 537
26
votes
7 answers

Interlocked.CompareExchange using GreaterThan or LessThan instead of equality

The System.Threading.Interlocked object allows for Addition (subtraction) and comparison as an atomic operation. It seems that a CompareExchange that just doesn't do equality but also GreaterThan/LessThan as an atomic comparison would be quite…
halfbit
  • 54,462
  • 46
  • 195
  • 426
25
votes
10 answers

Reading interlocked variables

Assume: A. C++ under WIN32. B. A properly aligned volatile integer incremented and decremented using InterlockedIncrement() and InterlockedDecrement(). __declspec (align(8)) volatile LONG _ServerState = 0; If I want to simply read _ServerState, do…
MattJ
  • 401
  • 2
  • 6
  • 7
23
votes
6 answers

Performance of Interlocked.Increment

Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms?
SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
21
votes
3 answers

What is Interlocked.Increment actually doing?

Interlocked.Increment seems like among the most standard/simple of operations one would need to perform in multithreaded code. I assume that the functionality of the method is some sort pattern that anyone with threading experience would be able to…
Maxim Gershkovich
  • 42,124
  • 39
  • 134
  • 227
20
votes
0 answers

Using await inside Interlocked.Exchange crashes the C# compiler

Ignore for a moment the absurdity of awaiting an Enumerable.Range call. It's just there to elicit the crash-y behavior. It just as easily could be a method that's doing some network IO to build a collection of value objects. (Indeed, this was where…
rianjs
  • 7,317
  • 5
  • 21
  • 36
19
votes
3 answers

Where is InterlockedRead?

Win32 api has a set of InterlockedXXX functions to atomically and synchronously manipulate simple variables, however there doesn't seem to be any InterlockedRead function, to simply retrive the value of the variable. How come? MSDN says…
Sause
17
votes
5 answers

C# fundamentally not portable?

I've been using C# for a while, and have recently started working on adding parallelism to a side project of mine. So, according to Microsoft, reads and writes to ints and even floats are atomic I'm sure these atomicity requirements workout just…
Jake
  • 181
  • 1
  • 5
17
votes
5 answers

Interlocked.CompareExchange with enum

I'm trying to use Interlocked.CompareExchange with this enum: public enum State { Idle, Running, //... } The following code doesn't compile, but that's what I want do do: if (Interlocked.CompareExchange(ref state, State.Running,…
joe
  • 7,264
  • 7
  • 45
  • 73
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
  • 202,011
  • 255
  • 600
  • 878
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,727
  • 13
  • 36
12
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,045
  • 2
  • 9
  • 13
1
2 3
13 14