BT

Actors Made Easy with C# and TPL DataFlow

by Jonathan Allen on Nov 19, 2013 |

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.

Hello stranger!

You need to Register an InfoQ account or 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

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2013 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT