libactor - a C library based on the Actor model

Chris Moos

October 28, 2009

Categories: Technology
Tags: actor concurrency c library

Recently I’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 all of the threading issues, so you don’t have to worry about any of it at all.

In a future version I plan to add more sandboxing to the actors, so that when one actor crashes, they don’t all go down!

Right now it supports the following:

Here is what a simple ping/pong example looks like:

#include <stdio.h>
#include <libactor/actor.h>

void *pong_func(void *args) {
        actor_msg_t *msg;

        while(1) {
                msg = actor_receive();
                if(msg->type == PING_MSG) {
                        printf("PING! ");
                        actor_reply_msg(msg, PONG_MSG, NULL, 0);

void *ping_func(void *args) {
        actor_msg_t *msg;
        actor_id aid = spawn_actor(pong_func, NULL);
        while(1) {
                actor_send_msg(aid, PING_MSG, NULL, 0);
                msg = actor_receive();
                if(msg->type == PONG_MSG) printf("PONG!\n");

void *main_func(void *args) {
        struct actor_main *main = (struct actor_main*)args;
        int x;

        /* Accessing the arguments passed to the application */
        printf("Number of arguments: %d\n", main->argc);
        for(x = 0; x < main->argc; x++) printf("Argument: %s\n", main->argv[x]);

        /* PING/PONG example */
        spawn_actor(ping_func, NULL);


There is a more detailed example in the source distribution, located in the examples/ directory.

You can get the source here. Please, let me know what you think about it, it was really fun to write!

Documentation is available here:

comments powered by Disqus