BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Actors Made Easy with C# and TPL DataFlow

| by Jonathan Allen on Nov 19, 2013. Estimated reading time: 1 minute |

For some design problems actors can make all the difference. An Actor in an independent entity that can respond to and send messages. In a way it is like its own little program running inside another process. But building actors in languages that don’t natively support them can be difficult.

The main problem that comes up is thread management. You usually don’t want to hijack the message sender’s thread as that would just give you method calling semantics. And allocating a separate thread to each actor doesn’t scale up past a dozen or so threads before context switching becomes a real problem.

Johan Olsson solved this dilemma for C# by building his actor library on top of TPL DataFlow. The purpose of TPL DataFlow is to chain together series of operations using asynchronous messages. As messages pass from one Block to the next they may be buffered, grouped, split, transformed, or otherwise manipulated according to the semantic and performance needs of the application.

At the core of his framework is an abstract class called Actor encapsulating an ActionBlock. The ActionBlock handles all of the threading issues internally so that the developer doesn’t need to deal with threads or tasks. Thread-safety comes from the fact that ActionBlocks are serialized by default. If you send two messages to it at one time it will buffer the second message until the first has been processed. This can be changed using the ExecutionDataflowBlockOptions class.

You can read more about Johan Olsson’s work and see example code in his post titled An actor model implementation in C# using TPL DataFlow.

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 dont miss out on content that matters to you

BT