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
1
vote
0 answers

Does the Interlocked class work with FieldOffset?

I am trying to atomically assign two 32-bit values in C#. I have came up with a solution using FieldOffset on a struct and the 64-bit version of the Interlocked methods. I am wondering whether the following code will work as expected, or if any CPU…
Gary
  • 11
  • 2
  • 3
1
vote
1 answer

How to use interlocked variable for WaitOnAddress / WakeByAddressSingle?

Recently I've got to know WaitOnAddress, WakeByAddressSingle, and Interlocked* family of functions. While making test code as a part of understanding process, I've faced C28112 warning. Following code is the test code that generates C28112…
YoonSeok OH
  • 647
  • 2
  • 7
  • 15
1
vote
1 answer

Why declare a variable as volatile and use Interlocked on it at the same time?

I am reading Joe Duffy's Concurrent Programming on Windows. At the end of the chapter "Memory Models and Lock Freedom", he gives an example of the lock free stack. I have gone through the code and there's one thing I don't understand, that is the…
Thanuja Dilhan
  • 137
  • 2
  • 10
1
vote
3 answers

Can Interlocked CompareExchange be used correctly in this multithreaded round-robin implementation?

I need to round-robin some calls between N different connections because of some rate limits in a multithreaded context. I've decided to implement this functionality using a list and a "counter," which is supposed to "jump by one" between instances…
1
vote
1 answer

Should InterlockedExchange be used on all setting of a variable?

I'm using InterlockedExchange in Windows and I have two questions that put together are basically my title question. InterlockedExchange uses type LONG (32-bits). According to Microsoft's documentation Interlocked Variable Access: "simple reads and…
newguy
  • 617
  • 6
  • 15
1
vote
0 answers

Validity of InterlockedCompareExchange64 + Read32 operations mix

Objective: I have a chunk of memory shared between several x86/x64 processes/threads. Inside it I have QWORD (64 bit) variable aligned on 8-byte boundary. That variable is accessed by one writer and several readers simultaneously, mix of x86 and x64…
dj_alek
  • 31
  • 4
1
vote
1 answer

Why Interlocked.CompareExchange is not working?

if (Interlocked.CompareExchange(ref this.popcount, this.popcount + 1, this.popcount) == this.popcount) { pop = Interlocked.CompareExchange(ref head, head.next, head); } I write a code but somehow it's not working as I…
cantcode
  • 11
  • 5
1
vote
2 answers

Encountering unexpected Interlocked behaviour?

I'm writing a web link checker program and encountering behaviour with Interlocked that I can't explain. First, here's an abridged version of the code: public class LinkCheckProcessor { private long _remainingLinks; public event…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
1
vote
2 answers

Interlocked.CompareExchange single-threaded equivalent code

I don't know why, but I can't quite seem to wrap my head around what's going on in Interlocked.CompareExchange(ref int a, int b, int c). Could someone show me what it would be doing, if it were just naively implemented in a single-threaded…
Brondahl
  • 7,402
  • 5
  • 45
  • 74
1
vote
3 answers

Does lock() makes sense here?

I have a simple class like : public class XXX { double val1; double val2; double delta; public void SetValues(double v1, double v2) { val1 = v1; val2 = v2; delta = val1 - val2; } public double Val1…
ali_bahoo
  • 4,732
  • 6
  • 41
  • 63
1
vote
1 answer

Diverse output in parallel code

It prints correct result 4.5 with the same probability as 0.1, 0.2, 1, 2.3 and etc. What is wrong with it? double average = 0; Parallel.ForEach( Enumerable.Range(0, 10), () => 0, (elem, loopState, localSum) => { localSum +=…
user8531685
1
vote
3 answers

How first entered thread can signal to other concurrent threads the end of same method?

How first entered thread can signal to other concurrent threads the end of same method ? I have method named say PollDPRAM(). It must make a trip over network to some slow hardware and refresh object private data. If the same method is…
user215054
1
vote
2 answers

How can I use an array item with Interlocked.CompareExchange

I'm familiar with using Interlocked.CompareExchange() with plain objects. However I'd like to use it with the member of an array: string[] myArray = new string[] { "A", "B", "C" }; string myStr = (string) Interlocked.CompareExchange(ref myArray[0],…
Dan S
  • 9,139
  • 3
  • 37
  • 48
1
vote
3 answers

Interlocked ops vs XXX::atomic on Win32

What are the advantages and disadvantages of using Interlocked winapi functions instead of any library provides atomic operations on Win32 platform? Portability is not an issue.
ali_bahoo
  • 4,732
  • 6
  • 41
  • 63
1
vote
1 answer

Updating property of a singleton with Thread Safety

Our setup is: Asp.NET + MVC5 using AutoFac for DI. We have a class (which is a singleton) which is managing the access tokens for a variety of services. Every now and then, these tokens get too close to expiry (less then 10 minutes) and we request…
Mavi Domates
  • 4,262
  • 2
  • 26
  • 45