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
5
votes
3 answers

C# Interlocked functions as a lock mechanism?

While I was reading about ReaderWriterLockSlim lock mechanism , There was this guy who suggested that Interlock Functions can be used for a finer locking Also, I found here another answer from Marc : ...Writes make their change to a cloned copy,…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
5
votes
4 answers

Can someone help spot the errors in my low lock list?

I've written a low lock list in C++ on windows 32-bit. I'm getting BIG improvements over using critical sections but I'd like someone to sanity check that what I'm doing is correct and there aren't any errors in what I've done: #ifndef…
Goz
  • 61,365
  • 24
  • 124
  • 204
4
votes
4 answers

Reentrant Timer in Windows Service

I want to build a windows Service, which should execute different methods at different times. Its not about accuracy at all. Im using a system.timers.timer, and regulate the different methods to be executed within the Eventhandler-method with…
Timo Jak
  • 73
  • 1
  • 9
4
votes
1 answer

_InterlockedCompareExchange64 compiler intrinsic on Windows XP?

Based on the Microsoft documentation InterlockedCompareExchange64 is not available as a Windows API call until Windows Vista. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms683562(v=vs.85).aspx. However, it seems like the…
4
votes
1 answer

Difference between interlocked variable access (on boolean) and std::atomic_flag

I was wondering what the differences are between accessing a boolean value using Windows' interlockedXXX functions and using std::atomic_flag. To my knowledge, both of them are lock-less and you can't set or read an atomic_flag directly. I wonder…
Ben
  • 1,519
  • 23
  • 39
4
votes
0 answers

How are ref variables being captured in closure?

I have code like this (simplified): long counter = 0L; var t = Task.Run(async () => { Interlocked.Increment(ref counter); // Resharper: "Access to modified closure" await Task.Delay(500); // some work }); // ... t.Wait(); //…
greatvovan
  • 2,439
  • 23
  • 43
4
votes
2 answers

Meaning of "The sign is ignored" in _InterlockedCompareExchange documentation

The documentation for _InterlockedCompareExchange says for every parameter The sign is ignored. So does it mean that numbers like 0xffff and 0x7fff (for 16-bit version) will be considered equal by _InterlockedCompareExchange16 etc. by other width…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
4
votes
2 answers

Concurrent modification of double[][] elements without locking

I have a jagged double[][] array that may be modified concurrently by multiple threads. I should like to make it thread-safe, but if possible, without locks. The threads may well target the same element in the array, that is why the whole problem…
tethered.sun
  • 149
  • 3
  • 14
4
votes
0 answers

Do variables of interlocked functions have to declared as volatile in native C++

Is there a difference between : __declspec(align(8)) long long variable1; //assume shared among threads InterlockedIncrement64(&variable1); And this: __declspec(align(8)) long long volatile variable2;//assume shared among threads …
David Jones
  • 149
  • 13
4
votes
2 answers

Interlocked.Read/Exchange for longs on 64-bit architectures

Is Interlocked.Read(ref long) "optimized away" on 64-bit architectures? I.e. if I am writing a library which could be used by both architectures, should I be concerned about performance impact of using Interlocked.Read unnecessarily on 64-bit…
Lou
  • 4,244
  • 3
  • 33
  • 72
4
votes
2 answers

Lock-free, awaitable, exclusive access methods

I have a thread safe class which uses a particular resource that needs to be accessed exclusively. In my assessment it does not make sense to have the callers of various methods block on a Monitor.Enter or await a SemaphoreSlim in order to access…
LaFleur
  • 145
  • 1
  • 7
4
votes
1 answer

Using Interlocked

Is this code thread-safe? or put it this way: Is there anyway to call GetIt() and that GetIt() will return the same number to 2 different threads Private Shared hitCount As Long = 1 Public Shared Function GetIt() As Long …
dr. evil
  • 26,944
  • 33
  • 131
  • 201
4
votes
1 answer

Interlocked.Increment vs lock in debug vs release mode

I was testing how Interlocked.Increment and lock behave on my computer's architecture because I read the following lines in this article. As rewritten with Interlocked.Increment, the method should execute faster, at least on some architectures.…
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
4
votes
2 answers

Should I use static fields and interlocked together?

Should I use static fields and interlocked together, in cases when i need to provide thread safety and atomic operations with static fields, Is static fields are atomic by default? For example: Interlocked.Increment(ref…
testCoder
  • 7,155
  • 13
  • 56
  • 75
3
votes
1 answer

Interlocked.Read / Interlocked.Exchange much slower on Mono than .NET?

Sorry for the long question, but there's a Jon Skeet reference, so it may be worthwhile for some. In short: Interlocked.Read / Interlocked.Exchange seem to perform much slower while running in the Mono framework than while running in the .NET…
qxn
  • 17,162
  • 3
  • 49
  • 72