BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Functional Programming and Coordination Data Structures

Functional Programming and Coordination Data Structures

Leia em Português

This item in japanese

Bookmarks

Coordination Data Structures, CDS, are a new set of thread-safe objects, mostly collections, planned for .NET 4. After about 6 months of silence, there have been some significant updates. Most notably is the inclusion of functional programming techniques to reduce the need for design patterns.

The first new class is the ConcurrentLinkedList. ConcurrentLinkedList uses a predicate function with the TryInsertBetween method to insert nodes in a thread-safe manner. The predicate function is called by the object itself to determine the right location, making this class easier to use than a normal LinkedList even in single-threaded scenarios.

Another place where functional techniques are being used is the SpinWait object. Rather than manually writing loops and calling SpinOnce over and over again, developers can pass a predicate to the SpinWait.SpinUntil method.

Next up is the new type Concurrent Bag is like a collection of queues that are load-balanced across threads. Normally each thread reads and writes to its own queue. However, if its queue is empty then it will steal items from another thread’s queue. This means most of the time there will be no contention across threads, but each thread can still run a full speed when there is work to be done. ConcurrentBag doesn’t replace ConcurrentQueue, which is still preferred in single producer/single consumer scenarios.

The WriteOnce class has been removed. This wasn’t a particularly interesting feature, little more than a object that throws an exception if the setter is called more than once. The far more useful Lazy and LazyVariable classes are being kept. Both support the idea of deferred initialization, also known as futures, The former is a class and the later a lightweight and slightly less safe structure. Also added are LazyInitializer, for times when the memory footprint is an issue, and ThreadLocal, a type-safe way of combining thread local storage with futures.

The ability to cancel tasks, without Thread-Aborts, continues to be an important issue. To help support this cancellation overloads have been added to all of the methods than may block. Microsoft is working to make the cancellation model ubiquitous, which would greatly simply library development.

For more information refer to Microsoft’s Parallel Programming blog and our earlier coverage of CDS.

Rate this Article

Adoption
Style

BT