views:

650

answers:

5

I am new to .net and would like to know whether .net has the java equivalent of AtomicInteger, ConcurrentLinkedQueue, etc?

I did a bit of search and couldnt come up with anything.

The lock free algorithms need some sort of a CAS instruction, which is provided through the undocumented Unsafe class in Java, does .net have anything equivalent?

+4  A: 

In .NET there is the Interlocked class, with static methods Interlocked.Increment() and Interlocked.Decrement().

See http://msdn.microsoft.com/en-us/library/system.threading.interlocked.aspx.

You will also find other atomic och synchronization constructs in the System.Threading namespace.

Tommy
+2  A: 

The Interlocked class has all the static methods needed to do simple atomic operations like increment, decrement, compare, swap, etc. Check out http://msdn.microsoft.com/en-us/library/system.threading.interlocked_members.aspx

For most collections you can get a synchronized collection through a static member called "Synchronized". Note however these aren't lock free constructs, they just hide the messiness of using locks/semaphores. Check the queue collection's synchronized method http://msdn.microsoft.com/en-us/library/system.collections.queue.synchronized.aspx

Darwyn
+3  A: 

I've written a good deal of lock free immutable collection structures in .Net. This includes, binary trees, maps, arrays, linked list, etc ... The source and binaries are available on code gallery

RantPack

JaredPar
savia. thanks mate.
Jonathan C Dickinson
A: 

For info, it is likely (here) that .NET 4.0 will inherit the CCR/TPL from Parallel Extensions. TPL, in particular, introduces a range of collections and other constructs designed for advanced threading scenarios (with minimal locks etc).

For now, there are a limited number of threaded collections etc, plus the usual locking primatives, plus Interlocked, etc.

Marc Gravell
A: 

Here is the problem I see with .net's Interlocked class.

I have multiple threads updating a counter. Each thread must get a unique value of the counter, thus no threads must get the same value.

The way the interlocked class in .net works, i have -

int counter;
void code(){
    myThreadVal = Interlocked.increment(counter);
}

now since both threads can see the same value of the counter, they both can get the same value of myThreadVal.

However, in the case of java's AtomicInteger that would never happen, each thread would always get a different value.

pdeva
ahh, so you mean Thread Local Storage?
Mitch Wheat
no. not thread local, since this counter is shared among multiple threads.
pdeva
No, two threads will never see the same value of myThreadValue. Interlocked.Increment is atomic. However, you need to pass counter by ref.
Jon Skeet
Ditto to Jon; this should result in different values per thread, unless you have something *else* going on (for example "myThreadVal" is shared between the threads, which would obviously cause conflicts).
Marc Gravell
i understand that Interlocked.increment is atomic but the 'counter' passed to it may be the same for both threads. Thus if 2 threads increment the same counter they both would get the same value even though the increment itself might be atomic.
pdeva
@pdeva: they will not get the same value if they're passing the same counter (unless an intervening Interlocked.Decrement() occurs) - that's the whole point of the interlocked increment...
Michael Burr
I believe what you're trying to do can be achieved by making your counter variable *static*, so that it is shared amongst all threads. Otherwise each thread gets its own counter, starting from zero. What you want is one global (static) counter shared between all threads, and allocating a unique sequential value to each thread.
Daniel Fortunov
@Daniel: Although the code snipper doesn't show it, I think it's safe to assume that we all interpreted `counter` to be static. The `Interlocked.Increment` is needed, because it would otherwise be possible for two threads to get the same value due to a race condition between the load and store operations.
Steven Sudit