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
10
votes
6 answers

C# Interlocked Exchange

I have a bit of my game which looks like this: public static float Time; float someValue = 123; Interlocked.Exchange(ref Time, someValue); I want to change Time to be a Uint32; however, when I try to use UInt32 instead of float for the values, it…
Martin
  • 12,469
  • 13
  • 64
  • 128
9
votes
3 answers

How to: Write a thread-safe method that may only be called once?

I'm attempting to write a thread-safe method which may only be called once (per object instance). An exception should be thrown if it has been called before. I have come up with two solutions. Are they both correct? If not, what's wrong with…
9
votes
2 answers

Fields read from/written by several threads, Interlocked vs. volatile

There are a fair share of questions about Interlocked vs. volatile here on SO, I understand and know the concepts of volatile (no re-ordering, always reading from memory, etc.) and I am aware of how Interlocked works in that it performs an atomic…
thr
  • 19,160
  • 23
  • 93
  • 130
9
votes
4 answers

InterlockedIncrement usage

While reading about the function InterlockedIncrement I saw the remark that the variable passed must be aligned on a 32-bit boundary. Normally I have seen the code which uses the InterlockedIncrement like this: class A { public: A(); void…
Naveen
  • 74,600
  • 47
  • 176
  • 233
9
votes
5 answers

Why use SyncLocks in .NET for simple operations when Interlocked class is available?

I've been doing simple multi-threading in VB.NET for a while, and have just gotten into my first large multi-threaded project. I've always done everything using the Synclock statement because I didn't think there was a better way. I just learned…
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
9
votes
1 answer

How does Interlocked work and why is it faster than lock?

I just learned of interlocked class and that it is supposed to be faster than simply locking. Now, this is all nice and good, but I'm curious as to implementation. As far as I know, the only way to ensure that operation on a variable is done…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
9
votes
2 answers

Are Interlocked* functions useful on shared memory?

Two Windows processes have memory mapped the same shared file. If the file consists of counters, is it appropriate to use the Interlocked* functions (like InterlockedIncrement) to update those counters? Will those synchronize access across…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
8
votes
2 answers

What's the difference between InterlockedCompareExchange Release() and Acquire()?

What's the difference between InterlockedCompareExchangeRelease() and InterlockedCompareExchangeAcquire()? When I try to learn the synchronization functions with WIN32 API, I find there are two functions named differently but seems to do the same…
Celebi
  • 1,280
  • 3
  • 16
  • 25
8
votes
4 answers

InterlockedExchange and memory alignment

I am confused that Microsoft says memory alignment is required for InterlockedExchange however, Intel documentation says that memory alignment is not required for LOCK. Am i missing something, or whatever? thanks from Microsoft MSDN Library Platform…
Patogenic
8
votes
4 answers

InterlockedExchange and memory visibility

I have read the article Synchronization and Multiprocessor Issues and I have a question about InterlockedCompareExchange and InterlockedExchange. The question is actually about the last example in the article. They have two variables iValue and…
user184968
8
votes
1 answer

Implementing a bitwise operation using the Interlocked class in .NET

I'm trying to set bit flags in a shared variable within a multithreaded .NET application, but couldn't find a parallell to the native InterlockedOr function in the managed Interlocked class. I've come up with the following code for performing a |=…
Cryovat
  • 166
  • 6
8
votes
5 answers

Is there any advantage of using volatile keyword in contrast to use the Interlocked class?

In other words, can I do something with a volatile variable that could not also be solved with a normal variable and the Interlocked class?
codymanix
  • 28,510
  • 21
  • 92
  • 151
8
votes
1 answer

why InterlockedAdd is not available in vs2010?

I have include windows.h and want to use InterlockedAdd in vs2010 and compiles error "identifier not found", but the InterlockedIncrement can work well. I try to use: #include #pragma intrinsic(_InterlockedAdd) and compiles error: …
magicyang
  • 453
  • 1
  • 5
  • 14
7
votes
4 answers

Interlocked Exchange of a struct

I want to use InterlockedExchange from the WinAPI to use a lock free synchronization of threads. At the moment I have a class like this. struct DataExchange { volatile LONG m_value; void SetValue(LONG newVal) { …
mkaes
  • 13,781
  • 10
  • 52
  • 72
7
votes
2 answers

Interlocked.Exchange slower than Interlocked.CompareExchange?

I came across some odd performance results when optimizing a program, which are shown in the following BenchmarkDotNet benchmark: string _s, _y = "yo"; [Benchmark] public void Exchange() => Interlocked.Exchange(ref _s, null); [Benchmark] public…
Shay Rojansky
  • 15,357
  • 2
  • 40
  • 69
1 2
3
15 16