<?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; c</title>
	<atom:link href="http://chrismoos.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrismoos.com</link>
	<description>developer by day, developer by night</description>
	<lastBuildDate>Fri, 30 Jul 2010 04:52:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 handles [...]]]></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="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;libactor/actor.h&gt;</span>
&nbsp;
<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>
        actor_msg_t <span style="color: #339933;">*</span>msg<span style="color: #339933;">;</span>
&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>
                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>
                <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>
                        <span style="color: #000066;">printf</span><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>
                        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>
                <span style="color: #009900;">&#125;</span>
                arelease<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<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>
        actor_msg_t <span style="color: #339933;">*</span>msg<span style="color: #339933;">;</span>
        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>
        <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>
                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>
                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>
                <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> <span style="color: #000066;">printf</span><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>
                arelease<span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                sleep<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<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>
        <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>
        <span style="color: #993333;">int</span> x<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* Accessing the arguments passed to the application */</span>
        <span style="color: #000066;">printf</span><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>
        <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> <span style="color: #000066;">printf</span><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>
&nbsp;
        <span style="color: #808080; font-style: italic;">/* PING/PONG example */</span>
        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>
<span style="color: #009900;">&#125;</span>
&nbsp;
DECLARE_ACTOR_MAIN<span style="color: #009900;">&#40;</span>main_func<span style="color: #009900;">&#41;</span></pre></div></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>23</slash:comments>
		</item>
	</channel>
</rss>
