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
3
votes
1 answer

how to perform atomic read for double?

Why is there no Interlocked.Read function available for double since there are Interlocked.Exchange and Interlocked.CompareExchange methods available for double which can perform more complex functions atomically. Using such methods to read a double…
Alok
  • 3,160
  • 3
  • 28
  • 47
3
votes
3 answers

Interlocked.Increment not thread safe?

I found a compiler bug in just one line of code: int thisIndex = Interlocked.Increment(ref messagesIndex) & indexMask; The definitions are: static int messagesIndex = -1; public const int MaxMessages = 0x10000; const int indexMask =…
Peter Huber
  • 3,052
  • 2
  • 30
  • 42
3
votes
1 answer

Interlocked average (CAS) not working on HLSL

Hy everyone. I tried to implement an interlocked moving average, using InterlockedCompareExchange, on HLSL, but I got the GPU stuck on an infinite loop. So, the code is this: [allow_uav_condition] while (true) { // Get old value uint old =…
Santiago Pacheco
  • 197
  • 1
  • 13
3
votes
1 answer

How safe are Interlocked.Exchange?

Beeing a threading noob, I'm trying to find a way w/o locking objects that allows me to enqueue a threadpool task, in such way that it has a max degree of parallelism = 1. Will this code do what I think it does? private int status; private const int…
Roger Johansson
  • 22,764
  • 18
  • 97
  • 193
3
votes
1 answer

Interlocked hanging game?

Just attempting to use Interlocked.Increment(ref threads) and it just hangs my game. public static int threads; ... Interlocked.Increment(ref threads); Using System.Threads also. I've moved it into the main thread and out of it, can't seem to…
Ken Rea
  • 67
  • 8
3
votes
3 answers

Does Interlocked.CompareExchange(double,double,double) work in 32 bit OS?

I'm maintaining a high performance class that can be operated on by multiple threads. Many of the fields are volatile ints, and as it turns out I need to upgrade one of those to a double. I'm curious if there is a lock free way to do this, and was…
Greg Graham
  • 473
  • 7
  • 18
3
votes
1 answer

Interlocked.Exchange can't be used with generics?

I'm writing a generic class where I need to use Interlocked. T test1, test2; Interlocked.Exchange(ref test1, test2); This won't compile. So am I forced to use Exchange(Object, Object) instead even tho MSDN advices not to use it that way?
remdao
  • 885
  • 3
  • 17
  • 23
3
votes
1 answer

Erroneous behavior when using Interlocked.Decrement along with monitor.wait and monitor.pulse in a multithreaded environment

I am trying to implement a multithreaded library that would run simultaneous tasks using threadpool. Basically it will add tasks to threadpool from the collection parameter it receive and then will wait until last task that is being processed sends…
Ferhat
  • 63
  • 6
3
votes
3 answers

Interlocked.Exchange clarification

I have a few simple (hopefully) questions that I have been unable to find answers for - Say I have objects a, b that are accessible to multiple threads. Interlocked.Exchange(ref a, b) If 'b' is not volatile, will this operation treat it as such?…
user981225
  • 8,762
  • 6
  • 32
  • 41
3
votes
2 answers

When should & shouldn't I use this C# utility class to control threads via Interlocked

I'm trying to understand the logic behind how this class was written, and when I should and shouldn't use it. Any insight would be appreciated internal struct SpinLock { private volatile int lockHeld; private readonly static int…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
2
votes
1 answer

InterlockedExchange Visual Studio 2010 Intrinsic

I have intrinsics enabled in the optimization settings for the compiler, however, the resulting code for InterlockedExchange is generating calls into kernel32.dll rather than producing inline assembly. This is especially problematic because the…
user1157123
2
votes
2 answers

Interlocked Class: read before write race condition problem

using System; using System.Threading; using System.Threading.Tasks; namespace InterlockedLearning { class Program { static int sharedVariable = 0; static void Main() { Parallel.For(0, 1000000, Func1); …
2
votes
1 answer

Is it safe to use the Interlocked.CompareExchange as a lock, only for enter, not for exit?

I found some multithreaded code in the quite popular LazyCache library, that uses an int[] field as a granular locking mechanism, with the intention to prevent concurrent invocation of a method with the same key as argument. I am highly skeptical…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
2
votes
2 answers

Proper way to synchronize a property's value in a multi-threaded application

I've recently started revisiting some of my old multi-threaded code and wondering if it's all safe and correct (No issues in production yet...). In particular am I handling object references correctly? I've read a ton of examples using simple…
2
votes
1 answer

Is there any reason to prefer Interlocked over volatile for an Immutable Collection where updates do not depend on previous values?

I noticed that in questions discussing immutable collections (e.g. What is the preferred method of updating a reference to an immutable object?) it was advised to use Interlocked (or better ImmutableInterlocked). The reason for this seems to be for…
asdf
  • 35
  • 3