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

Test and conditionally update a long using Interlocked

Is there a neat way to do this using the Interlocked class? Or should I just use lock { }? My specific use case is that I have multiple threads that compute a long value, and compare it to a shared "Maximum" value, replacing the shared value only if…
Rob
  • 4,327
  • 6
  • 29
  • 55
2
votes
0 answers

Is there a way to perform an atomic (read-update-write) OR operation on two bytes in C#?

I looked at the Interlocked.Or method in .NET 5. This is great for two integer values. Is there a way to perform the equivalent of this on two separate byte values? I searched the documentation, and I can see that InterlockedOr8 exists in winnt.h,…
Alexandru
  • 12,264
  • 17
  • 113
  • 208
2
votes
3 answers

Can Interlocked.Exchange exchange two byte[] arrays?

I want to swap two byte arrays atomically, without the need for a lock. i.e. I don't want to do byte[] src; byte[] dest; lock(synchLock) { dest = src; } Is this possible with Interlocked.Exchange ? I see it works for…
Jacko
  • 12,665
  • 18
  • 75
  • 126
2
votes
1 answer

Interlocked.CompareExchange instruction reodering of the initialvalue

Iam wondering if its possible that the initialvalue in the following code can be reordered to be after the computation resulting in undefined behavior. The following example is taken from…
2
votes
1 answer

improve atomic read from InterlockedCompareExchange()

Assuming architecture is ARM64 or x86-64. I want to make sure if these two are equivalent: a = _InterlockedCompareExchange64((__int64*)p, 0, 0); MyBarrier(); a = *(volatile __int64*)p; MyBarrier(); Where MyBarrier() is a memory barrier (hint) of…
cozmoz
  • 29
  • 3
2
votes
1 answer

How to update my model counter value using Interlocked.Add

So I have this model: public class Container : INotifyPropertyChanged { private int _total; private static InjectionContainer _mainContainer = new InjectionContainer(); private static InjectionContainer _secondContainer = new…
Dean Movy
  • 139
  • 1
  • 9
2
votes
0 answers

long vs {0L}[0]

In one of our old services I found such piece of code (comments are original): long[] tasksCounter = {0}; //boxing for long counters long[] errorsCounter = {0}; //boxing for long counters Further in the code these "arrays" are used with…
greatvovan
  • 2,439
  • 23
  • 43
2
votes
2 answers

very strange and severe multithread inconsistency problem c#

I have a very simple watchdog program with 2 threads. One thread is updating a long variable and the other thread reads the variable. and alert if it was more than X seconds from the last update. The problem is that sometimes (happens once a day…
ofer alper
  • 71
  • 3
2
votes
3 answers

Can I use `Semaphore.Release` as a readymade interlocked increment?

I need a cross-process counter. I could map an integer in a memory-mapped file and use Interlocked.Increment on it, but it seems to me that Semaphore.Release would do the same thing and with less programming overhead. For example: var mySem = new…
dan-gph
  • 16,301
  • 12
  • 61
  • 79
2
votes
1 answer

c - one thread writes a variable, the other reads it

What is the correct way in C to make sure that when one thread writes to a variable the other thread can read the updated value? I didn't find any answers to this despite searching and 'Questions that may already have your answer', which gave me…
newguy
  • 617
  • 6
  • 15
2
votes
1 answer

What is the Java equivalent for C# `Interlocked.Exchange(Object, Object) : Object`?

What is the Java equivalent for C# Interlocked.Exchange(Object, Object) : Object? Is there a way in Java to perform the following actions in a single atomic step without lock: 1) store locally the reference of a variable 2) set another reference to…
jeromerg
  • 2,997
  • 2
  • 26
  • 36
2
votes
6 answers

C# Object Pooling With Interlocked.Increment

I have seen many good object pool implementations. For example: C# Object Pooling Pattern implementation. But it seems like the thread-safe ones always use a lock and never try to use Interlocked.* operations. It seems easy to write one that…
Michael Covelli
  • 2,113
  • 4
  • 27
  • 41
2
votes
0 answers

Way to do cross-platform interlocked options that also allows me to optionally bypass the synchronization?

With std::atomic, there seems to be no standards-compliant way to sometimes read/write without atomicity. Boost has interlocked operations, but they are in the details namespace so I don't think I'm supposed to use it. But I don't know all of boost.…
VoidStar
  • 5,241
  • 1
  • 31
  • 45
2
votes
1 answer

Best, thread safe way to access a field once i.e. for disposal

Basically I want to make sure a field in a class (in this case the _changedPoller) is Disposed as early as possible when not needed anymore. I call StopChangedPolling in the class' Dispose method and when certain events occur. What is the best,…
atlemann
  • 173
  • 1
  • 7
2
votes
1 answer

InterlockedDecrement uses XADD but InterlockedIncrement uses INC?

I am debugging some code using a boost C++ library, which uses Windows InterlockedDecrement and InterlockedIncrement. In the outputted assembly InterlockedIncrement uses LOCK INC whereas the InterlockedDecrement uses LOCK XADD. Why do they not both…
intrigued_66
  • 16,082
  • 51
  • 118
  • 189