interlocked

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 Threading.Interlocked.Increment(hitCount) DoSomethingQuick(hitCount) Return hitCount End Function ...

Why does Interlocked.CompareExchange<T> only support reference types?

Disclaimer: My posts are apparently always verbose. If you happen to know the answer to the title question, feel free to just answer it without reading my extended discussion below. The System.Threading.Interlocked class provides some very useful methods to assist in writing thread-safe code. One of the more complex methods is Compare...

Does Interlocked guarantee visibility to other threads in C# or do I still have to use volatile?

I've been reading the answer to a similar question, but I'm still a little confused... Abel had a great answer, but this is the part that I'm unsure about: ...declaring a variable volatile makes it volatile for every single access. It is impossible to force this behavior any other way, hence volatile cannot be replaced with Int...

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 about the Interlocked Class - it makes it look as though all this: Private SomeInt as Integ...

Is this a correct Interlocked synchronization design?

I have a system that takes Samples. I have multiple client threads in the application that are interested in these Samples, but the actual process of taking a Sample can only occur in one context. It's fast enough that it's okay for it to block the calling process until Sampling is done, but slow enough that I don't want multiple threa...

locking mechanism that provides information about the state in .NET 3.5

I'm trying to find a way to provide exclusive access to a resource, while at the same time providing information about the state of the lock (_isWorking) for other classes to read. Here's what I have come up with so far : private int _isWorking = 0; public bool IsWorking { get { return Interlocked.CompareExchange(r...

Win API Interlocked operations for 32-bit int type

If we have: __int32 some_var = 0; What is the best (if any) way to call InterlockedExchange, InterlockedIncrement and other interlocked functions which require LONG* for some_var ? Since, there is guarantee that LONG is 32 bit on any Windows, it's probably safe just to pass (long*) some_var. However, it seems to me quite ugly and I c...

C# Object Pooling With Interlocked.Increment

I have seen many good object pool implementations. For example: http://stackoverflow.com/questions/2510975/c-object-pooling-pattern-implementation. But it seems like the thread-safe ones always use a lock and never try to use Interlocked.* operations. It seems easy to write one that doesn't allow returning objects to the pool (just ...

C# fundamentally not portable?

I've been using C# for a while, and have recently started working on adding parallelism to a side project of mine. So, according to Microsoft, reads and writes to ints and even floats are atomic I'm sure these atomicity requirements workout just fine on x86 architectures. However, on architectures such as ARM (which may not have hardw...