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

Thread-safe left shift

The obvious way to do it would be with locking. But I know that there is Interlocked class in c#, which is good for thread safe increment and decrement, so I wondered if there is something like that which would let me do the same for binary…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
2
votes
2 answers

Parallel version of code to accumulate values from list>

I am again struggling to understand some things for parallel computation. In the code I am working on, I have a class that extends list>. Inside this class, I'm writing a method to return the average of the values in the…
Bovaz
  • 375
  • 6
  • 20
2
votes
2 answers

How do I check for overflow after an Interlocked.Increment in C#?

What is the correct way to check for an overflow after a call to Interlocked.Increment? I have an ID generator that generates unique ids during the program's execution, and currently I test if the increment returned zero. public static class…
Suzanne Soy
  • 3,027
  • 6
  • 38
  • 56
2
votes
0 answers

extend Interlocked.Add as Interlocked.add(ref float,ref float) using interlocked.CompareExchange

public static class InterlockedEx { // AddToTotal safely adds a value to the running total. public static float Add(ref float totalValue,float addend) { float initialValue, computedValue; do { //…
huoxudong125
  • 1,966
  • 2
  • 26
  • 42
2
votes
6 answers

Is the C# "lock" construct rendered obsolete by Interlocked.CompareExchange?

Summary: It seems to me that: wrapping fields representing a logical state into a single immutable consumable object updating the object's authoritative reference with a call to Interlocked.CompareExchange and handling update failures…
Triynko
  • 18,766
  • 21
  • 107
  • 173
2
votes
1 answer

InterlockedExchange vs. InterlockedExchangePointer

What is the difference between InterlockedExchange and InterlockedExchangePointer? Are if( 0 != InterlockedCompareExchange( ( void** ) &_myVariable , temp , 0 …
Martin85
  • 308
  • 4
  • 13
2
votes
4 answers

Possible to use Interlocked.CompareExchange with a Bit?

I have an array of ints that track the completion of 10,000 concurrent tasks, where the value is either 1 or 0. I think it would be more efficient if this array was an array of Bits and each concurrent thread used interlocked.CompareExchange (or…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
2
votes
2 answers

Interlocked.CompareExchange if not equal?

Possible Duplicate: Interlocked.CompareExchange using GreaterThan or LessThan instead of equality I know that Interlocked.CompareExchange exchanges values only if the value and the comparand are equal, How to exchange them if they are not…
user1748906
  • 526
  • 4
  • 13
2
votes
2 answers

How can i implement a InterlockedIncrement without a memory barrier

I implement a reference counting schema and for this i need interlock facilities but no memory fences (as far as i understand). Unfortunately only Windows has a InterlockedDecrementNoFence compiler intrinsic. How can i do this with asm inlining? I…
Lothar
  • 12,537
  • 6
  • 72
  • 121
2
votes
0 answers

What sort of optimzations would a 64-bit C# Compiler/JITer perform on Interlocked operations on Int64s?

I am working some threaded library code that needs to support 32 and 64 bit codebases, so there are various interlocked calls throughout the contentious paths specifically to handle non-atomic loads and stores of Int64s. I am wondering, when…
Chuu
  • 4,301
  • 2
  • 28
  • 54
2
votes
2 answers

Will the threadpool queue a timer's callback function, sometimes scheduling more than one thread at the same time?

In the following code TimerRecalcStatisticsElapsed should only have one instance of it running. The worker methods that this callback invokes is made to run in sequence, with a maximum of one thread running at a time. Question Part 1: If the…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
1
vote
1 answer

C# How to use Interlocked.CompareExchange

My goal is the following: There is a certain range of integers, and I have to test every integer in that range for something random. I want to use multiple threads for this, and divide the work equally among the threads using a shared counter. I set…
Inigo
  • 341
  • 3
  • 12
1
vote
2 answers

Thread safe id generation with Interlocked in c#

I am trying to understand Interlocked in C# in thread synchronization. public int MethodUsedByMultipleThreads() { var id = CreateNextId(); return id; } private long CreateNextId() { long id = 0; Interlocked.Exchange(ref id ,…
Azeeb
  • 81
  • 11
1
vote
0 answers

Read int value in thread safe way

I have int counter And have increment it in multithread by Interlocked.Increment(ref counter); Is thread safe reading it in other thread (while others still incrementing it) like this: int localSaved = counter; or int localSaved =…
KeyKiller
  • 65
  • 4
1
vote
1 answer

Is it safe to use Volatile.Read combined with Interlocked.Exchange for concurrently accessing a shared memory location from multiple threads in .NET?

Experts on threading/concurrency/memory model in .NET, could you verify that the following code is correct under all circumstances (that is, regardless of OS, .NET runtime, CPU architecture, etc.)? class…
Adam Simon
  • 2,762
  • 16
  • 22