BT

Functional Programming and Coordination Data Structures

| by Jonathan Allen Follow 577 Followers on Mar 18, 2009. Estimated reading time: 1 minute |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT