interlocked

Volatile vs. Interlocked vs. lock

Let's say that a class has a public int counter field that is accessed by multiple threads. This int is only incremented or decremented. To increment this field, which approach should be used, and why? lock(this.locker) this.counter++; Interlocked.Increment(ref this.counter); Change the access modifier of counter to public volatile ...

Does Interlocked provide visibility in all threads?

Suppose I have a variable "counter", and there are several threads accessing and setting the value of "counter" by using Interlocked, i.e.: int value = Interlocked.Increment(ref counter); and int value = Interlocked.Decrement(ref counter); Can I assume that, the change made by Interlocked will be visible in all threads? If not, w...

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 f(); private: volatile long m_count; }; A::A() : m_count(0) { } void A::f() { ::Inte...

Reading interlocked variables

Assume: A. C++ under WIN32. B. A properly aligned volatile integer incremented and decremented using InterlockedIncrement() and InterlockedDecrement(). __declspec (align(8)) volatile LONG _ServerState = 0; If I want to simply read _ServerState, do I need to read the variable via an InterlockedXXX function? For instance, I have seen...

Is a lock (wait) free doubly linked list possible?

Asking this question with C# tag, but if it is possible, it should be possible in any language. Is it possible to implement a doubly linked list using Interlocked operations to provide no-wait locking? I would want to insert, add and remove, and clear without waiting. ...

interlocked operation on unanligned data

Hi; The win32 interlocked functions provide a mecanism for atomic operation on data. They are supposed to be thread-safe and multiprocessor-safe. What happen if the data is not aligned? the interlocked operations are still atomic? Ex.: incrementing a integer that is not aligned. Ty ...

Performance of Interlocked.Increment

Is Interlocked.Increment(ref x) faster or slower than x++ for ints and longs on various platforms? ...

Where is InterlockedRead?

Win32 api has a set of InterlockedXXX functions to atomically and synchronously manipulate simple variables, however there doesn't seem to be any InterlockedRead function, to simply retrive the value of the variable. How come? MSDN says that "Simple reads and writes to properly-aligned 32-bit variables are atomic operations", but adds t...

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 protests that the type must be a reference type. Float is not a reference type, so I know ...

What's Java's equivalent of .Net's Interlocked class?

How do I modify an int atomically and thread-safely in Java? Atomically increment, test & set, etc...? ...

Interlocked used to increment/mimick a boolean, is this safe?

Hi, I'm just wondering whether this code that a fellow developer (who has since left) is OK, I think he wanted to avoid putting a lock. Is there a performance difference between this and just using a straight forward lock? private long m_LayoutSuspended = 0; public void SuspendLayout() { Interlocked.Exchange(ref m_L...

Interlocked.Exchange can't be used with generics?

I'm writing a generic class where I need to use Interlocked. T test1, test2; Interlocked.Exchange<T>(ref test1, test2); This won't compile. So am I forced to use Exchange(Object, Object) instead even tho MSDN advices not to use it that way? ...

Is the C# "lock" construct rendered obselete by Interlocked.CompareExchange<T>?

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<T> and handling update failures appropriately provides a kind of concurrency that renders the "lock" construct not only unnecess...

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 processes? Or do I need to use something heavier, like a mutex? Or perhaps the shared-memory me...

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? ...

Threading and un-safe variables

I have code listed here: Threading and Sockets. The answer to that question was to modify isListening with volatile. As I remarked, that modifier allowed me to access the variable from another thread. After reading MSDN, I realized that I was reading isListening from the following newly created thread process. So, my questions now: ...

Safety of Interlock.Exchange and Garbage Collection

I have an object that I am accessing from two threads. One thread calls a long-running member function on the object that returns a value. The second thread updates the object used to produce that value. I if I call Interlock.Exchange to replace the object from the second thread while the first thread is executing: 1. Will the old th...

Lockless Deque in Win32 C++

I'm pretty new to lockless data structures, so for an exercise I wrote (What I hope functions as) a bounded lockless deque (No resizing yet, just want to get the base cases working). I'd just like to have some confirmation from people who know what they're doing as to whether I've got the right idea and/or how I might improve this. clas...

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

Hi all, 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 __LOW_LOCK_STACK_H_ #define __LOW_LOCK_STACK_H_ template< class T > class LowLockStack { pr...

Does Interlocked.CompareExchange(double,double,double) work in 32 bit OS?

I'm maintaining a high performance class that can be operated on by multiple threads. Many of the fields are volatile ints, and as it turns out I need to upgrade one of those to a double. I'm curious if there is a lock free way to do this, and was wondering if the Interlocked.CompareExchange(double, double, double) works as advertised ...