InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Functional Programming and Coordination Data Structures

Posted by Jonathan Allen on Mar 18, 2009

Sections
Development,
Architecture & Design
Topics
.NET ,
Performance & Scalability
Tags
Concurrency ,
Parallel Programming

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.

No comments

Watch Thread Reply

Educational Content

Jesper Boeg on Priming Kanban

In this interview, Jesper Boeg, author of the new InfoQ book – Priming Kanban, discusses the keys to using Kanban effectively, and how to get started if you are currently using other approaches.

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.