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
777
votes
10 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
  • 32,451
  • 45
  • 138
  • 193
56
votes
6 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
  • 2,113
  • 4
  • 27
  • 41
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,329
  • 2
  • 21
  • 37
38
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
  • 243,077
  • 51
  • 345
  • 550
31
votes
4 answers

Why is there no overload of Interlocked.Add that accepts Doubles as parameters?

I fully appreciate the atomicity that the Threading.Interlocked class provides; I don't understand, though, why the Add function only offers two overloads: one for Integers, another for Longs. Why not Doubles, or any other numeric type for that…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
29
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…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
28
votes
2 answers

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 In particular I need to update double item of…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
26
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
  • 411
  • 2
  • 6
  • 7
22
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
  • 45,951
  • 44
  • 147
  • 243
22
votes
5 answers

Thread safe DateTime update using Interlocked.*

Can I use an Interlocked.* synchronization method to update a DateTime variable? I wish to maintain a last-touch time stamp in memory. Multiple http threads will update the last touch DateTime variable. I appreciate that DateTime variables are value…
camelCase
  • 223
  • 1
  • 2
  • 4
22
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
  • 868,454
  • 176
  • 1,908
  • 1,964
21
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
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,767
  • 5
  • 24
  • 40
18
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
  • 8,344
  • 9
  • 54
  • 80
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
1
2 3
15 16