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

Parallel Programming Interlocked C#

Im confused with the example at http://msdn.microsoft.com/en-us/library/dd997393.aspx Parallel.ForEach(nums, // source collection () => 0, // method to initialize the local variable …
TheWommies
  • 4,922
  • 11
  • 61
  • 79
3
votes
1 answer

Thread-safe lockless memory pool: free function does not behave correctly in multi-thread

I have a simple implementation of a thread-safe allocator of same-sized buffers. Inside, the implementation is a very simple interlocked singly-linked list, that utilizes the unused space in unallocated buffers to maintain a singly-linked list. Also…
user2281752
  • 145
  • 1
  • 9
3
votes
1 answer

Is there some good reason for the return value of Interlocked.CompareExchange

The Interlocked.CompareExchange() method (docs) does roughly speaking this: "I have a variable, and I think I know what value it currently has. If I'm right, then please change the value to that". The point being that this method can be used to…
Brondahl
  • 7,402
  • 5
  • 45
  • 74
3
votes
2 answers

Fastest way to safely read contents of long[] whose elements are changed concurrently

When you have a long[] myArray = new long[256]; whose items are changed by multiple threads using Interlocked.Increment(ref myArray[x]) it sure won't be possible to get a snapshot of myArray at one point in time, as there are non-locked writes…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
3
votes
0 answers

Interlocked Functions Don't Seem to Work as Expected in a Multithreaded PowerShell Script

I tried to utilize thread-safe DotNET classes (like BlockingCollection, ConcurrentQueue) and static functions (like Interlocked::Increment()) in my multithreaded PowerShell script (achieved through RunSpacePool), but those seem to behave in…
3
votes
4 answers

WinAPI _Interlocked* intrinsic functions for char, short

I need to use _Interlocked*** function on char or short, but it takes long pointer as input. It seems that there is function _InterlockedExchange8, I don't see any documentation on that. Looks like this is undocumented feature. Also compiler wasn't…
ledokol
  • 133
  • 1
  • 7
3
votes
1 answer

C# - Interlocked Increment on a Dictionary Like List

I know that the int wont have a fixed position in memory so it simply cant work like that. But The exact same portion of code will be run concurrently with different names, parameters e.t.c I need to essentially pass a string of "Name" and then…
user8549339
3
votes
2 answers

Interlocked operations with acquire and release semantic (multi-platform)

EDIT: Okay, I've got a specific question. I want to implement 'exchange' functionality with acquire and release semantic (pseudo-code): interlocked_inc_32(target) { mov ecx, 1 lea eax, target lock xadd, [eax],…
0xbadf00d
  • 17,405
  • 15
  • 67
  • 107
3
votes
1 answer

In MSVC, why do InterlockedOr and InterlockedAnd generate a loop instead of a simple locked instruction?

On MSVC for x64 (19.10.25019), InterlockedOr(&g, 1) generates this code sequence: prefetchw BYTE PTR ?g@@3JC mov eax, DWORD PTR ?g@@3JC ; g npad 3 $LL3@f: mov ecx, eax or ecx, 1 lock…
3
votes
1 answer

Correct way of synchronization between a method and a stop functionality

I have a function (lets call it function A) that 0 to many threads can access it (at the same time, no shared resources). At any given time, the user can use to stop the process. The stop functionality needs to make sure that there are threads…
Albert Herd
  • 433
  • 1
  • 7
  • 13
3
votes
1 answer

Unsigned Interlocked Reads

What is the reasoning behind Interlocked.Read() only being defined for Int64 and not for UInt64? I wouldn't have thought there was any difference between the two types.
Nicholas
  • 1,392
  • 16
  • 38
3
votes
2 answers

What operations are called interlocked?

I am trying to make it clear for myself what "interlocked" exactly means. I read the following: "The interlocked functions provide a simple mechanism for synchronizing access to a variable that is shared by multiple threads. They also perform…
mentalmushroom
  • 2,261
  • 1
  • 26
  • 34
3
votes
2 answers

lock vs Interlocked.Exchange

I have an application which constantly (+-100ms) reads orders from a PLC and then puts them in a model which then gets read by multiple clients. For this im using the lock statement. Order Reading thread : lock (model) { //update object } Clients…
Christophe
  • 87
  • 1
  • 12
3
votes
2 answers

Atomic Operation Thread Safety - Do I Need a "Mirror" Atomic Read?

Is the following code safe (considering it in isolation) from a torn read? private static double flopsErrorMargin = 0.01d; public static double FlopsErrorMargin { get { double result = flopsErrorMargin; Thread.MemoryBarrier(); …
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
3
votes
1 answer

Timing of memory caches coherency after memory barrier and after Interlocked operations

Is there a difference in timing of memory caches coherency (or "flushing") after Interlocked operations and after invoking Memory barriers? Let's consider in C# - using any Interlocked operations vs Thread.MemoryBarrier() - is the resulting memory…
Jan
  • 1,905
  • 17
  • 41