<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>chris moos&#039;s blog &#187; concurrency</title>
	<atom:link href="http://chrismoos.com/tag/concurrency/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrismoos.com</link>
	<description>coding my way through life</description>
	<lastBuildDate>Sun, 18 Sep 2011 19:48:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>libactor &#8211; a C library based on the Actor&#160;model</title>
		<link>http://chrismoos.com/2009/10/28/libactor-a-c-library-based-on-the-actor-model/</link>
		<comments>http://chrismoos.com/2009/10/28/libactor-a-c-library-based-on-the-actor-model/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 06:38:38 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[actor]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=604</guid>
		<description><![CDATA[Recently I&#8217;ve been really interested in functional, concurrent programming languages, such as Erlang. This prompted me to have some fun in C, and try to implement a simple library that is based on the Actor Model. Right now it is usable, although it may not be ready for production. It uses pthreads and the library [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been really interested in functional, concurrent programming languages, such as Erlang. This prompted me to have some fun in C, and try to implement a simple library that is based on the <a href="http://en.wikipedia.org/wiki/Actor_model">Actor Model</a>.</p>
<p>Right now it is usable, although it may not be ready for production. It uses pthreads and the library handles all of the threading issues, so you don&#8217;t have to worry about any of it at all. </p>
<p>In a future version I plan to add more sandboxing to the actors, so that when one actor crashes, they don&#8217;t all go down!</p>
<p>Right now it supports the following:</p>
<ul>
<li>Spawn actor</li>
<li>Sending messages</li>
<li>Broadcast message</li>
<li>Actor memory management convenience functions(when an actor dies, the memory is freed!)</li>
</ul>
<p>Here is what a simple ping/pong example looks like:</p>
<div class="codecolorer-container c blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;libactor/actor.h&gt;</span><br />
<br />
<span style="color: #993333;">void</span> <span style="color: #339933;">*</span>pong_func<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; actor_msg_t <span style="color: #339933;">*</span>msg<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg <span style="color: #339933;">=</span> actor_receive<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">-&gt;</span>type <span style="color: #339933;">==</span> PING_MSG<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;PING! &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actor_reply_msg<span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">,</span> PONG_MSG<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arelease<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">void</span> <span style="color: #339933;">*</span>ping_func<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; actor_msg_t <span style="color: #339933;">*</span>msg<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; actor_id aid <span style="color: #339933;">=</span> spawn_actor<span style="color: #009900;">&#40;</span>pong_func<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actor_send_msg<span style="color: #009900;">&#40;</span>aid<span style="color: #339933;">,</span> PING_MSG<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msg <span style="color: #339933;">=</span> actor_receive<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">-&gt;</span>type <span style="color: #339933;">==</span> PONG_MSG<span style="color: #009900;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;PONG!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arelease<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sleep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #993333;">void</span> <span style="color: #339933;">*</span>main_func<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">struct</span> actor_main <span style="color: #339933;">*</span>main <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> actor_main<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>args<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> x<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Accessing the arguments passed to the application */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Number of arguments: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> main<span style="color: #339933;">-&gt;</span>argc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> main<span style="color: #339933;">-&gt;</span>argc<span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Argument: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> main<span style="color: #339933;">-&gt;</span>argv<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* PING/PONG example */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; spawn_actor<span style="color: #009900;">&#40;</span>ping_func<span style="color: #339933;">,</span> NULL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
DECLARE_ACTOR_MAIN<span style="color: #009900;">&#40;</span>main_func<span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>There is a more detailed example in the source distribution, located in the <em>examples/</em> directory.</p>
<p>You can get the source <a href="http://chrismoos.com/libactor-0.1.tar.gz">here</a>. Please, let me know what you think about it, it was really fun to write!</p>
<p>Documentation is available here: <a href="http://chrismoos.com/libactordocs/">http://chrismoos.com/libactordocs/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/10/28/libactor-a-c-library-based-on-the-actor-model/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

