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
7
votes
2 answers

Possible to create AtomicReference that can be swapped atomically?

Is there any way to implement a type of reference whose value can be exchanged with another atomically? In Java we have AtomicReference which can be swapped with a local variable but not with another AtomicReference. You can do: AtomicReference r1…
finnw
  • 47,861
  • 24
  • 143
  • 221
7
votes
2 answers

Even faster inexpensive thread-safe counter?

I've read this topic: C# Thread safe fast(est) counter and have implemented this feature in my parallel code. As far as I can see it all works fine, however it has measurably increased the processing time, as in 10% or so. It's been bugging me a…
mmix
  • 6,057
  • 3
  • 39
  • 65
7
votes
1 answer

How to use x64 Interlocked Operations against MemoryMappedFiles in .net

I need to use Interlocked Operations (CompareExchange, Increment etc.) against memory in MemoryMappedFiles in .NET. I found this answer to a very similar question. The problem is that Interlocked Operations are not exported from kernel32 (or any…
Jan
  • 1,905
  • 17
  • 41
7
votes
2 answers

Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?

I've been reading the answer to a similar question, but I'm still a little confused... Abel had a great answer, but this is the part that I'm unsure about: ...declaring a variable volatile makes it volatile for every single access. It is…
Kiril
  • 39,672
  • 31
  • 167
  • 226
7
votes
3 answers

Why does Interlocked.CompareExchange only support reference types?

Disclaimer: My posts are apparently always verbose. If you happen to know the answer to the title question, feel free to just answer it without reading my extended discussion below. The System.Threading.Interlocked class provides some very useful…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
7
votes
4 answers

How should I increment a number for a round robin threading scenario with least contention?

If many threads are calling GetNextNumber simultaneously with the following code, GetNextNumber will return 1 more times than any other numbers. private class RoundRobbinNumber { private int _maxNumbers = 10; private int _lastNumber; …
Eric Dahlvang
  • 8,252
  • 4
  • 29
  • 50
6
votes
2 answers

What does "late-bound access to the destination object" mean?

The docs for Interlocked.Exchange contain the following remark: This method overload is preferable to the Exchange(Object, Object) method overload, because the latter requires late-bound access to the destination object. I am quite bewildered…
Benjamin Hodgson
  • 42,952
  • 15
  • 108
  • 157
6
votes
1 answer

Is it safe to mix locks and interlock operations?

I have some code which must be thread safe whose behavior resembles this: protected long m_RunningValue protected long m_RunningCounter protected object m_Lock = new object(); public long RunningValue { get { return…
Chuu
  • 4,301
  • 2
  • 28
  • 54
6
votes
2 answers

Read an up-to date value from an Interlocked variable, with only one write on the variable

I would like to create a class with two methods: void SetValue(T value) stores a value, but only allows storing a single value (otherwise it throws an exception). T GetValue() retrieves the value (and throws an exception if there is no value…
Suzanne Soy
  • 3,027
  • 6
  • 38
  • 56
5
votes
3 answers

How does this MSDN CompareExchange sample not need a volatile read?

I was looking for a thread-safe counter implementation using Interlocked that supported incrementing by arbitrary values, and found this sample straight from the Interlocked.CompareExchange documentation (slightly changed for simplicity): private…
Ani
  • 111,048
  • 26
  • 262
  • 307
5
votes
1 answer

CS8625 Cannot convert null literal to non-nullable reference type warning for Interlocked.Exchange(ref c, null)

The following code works properly in .NET core 3.1, but generates wrongly the warning CS8625 Cannot convert null literal to non-nullable reference type: #nullable enable using System.Threading; namespace InterlockedExchangeNullProblem { public…
Peter Huber
  • 3,052
  • 2
  • 30
  • 42
5
votes
2 answers

Lockless using InterlockedCompareExchange

I am trying to make following snip of code lockless using interlocked operations, Any idea how to translate this? if (m_Ref == 0xFFFF) m_Ref = 1; else { if (++m_Ref == 1) CallSomething(); // } I was thinking something like if…
Suresh
  • 183
  • 1
  • 1
  • 5
5
votes
0 answers

How to perform aligned Interlocked.CompareExchange of Int64 in x86

I almost went crazy when trying to debug a random 40x performance drop when running in x86 on an algorithm which make heavy use of Interlock.CompareExchange with an Int64. I finally isolated the issue and it occurred only when the said Int64 was not…
Jeff Cyr
  • 4,774
  • 1
  • 28
  • 42
5
votes
2 answers

Is this a correct Interlocked synchronization design?

I have a system that takes Samples. I have multiple client threads in the application that are interested in these Samples, but the actual process of taking a Sample can only occur in one context. It's fast enough that it's okay for it to block…
Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
5
votes
1 answer

GCC atomic builtins, port to Windows/Visual Studio

I am working on a project that depends on GCC's atomic built-ins. While porting the project to Windows, I was trying to find the Interlocked* counterparts for GCC atomics. Most operations are actually available, but I miss operations that apply…
Sebastian B.
  • 287
  • 4
  • 19