libactor – a C library based on the Actor model

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:

  • Spawn actor
  • Sending messages
  • Broadcast message
  • Actor memory management convenience functions(when an actor dies, the memory is freed!)

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);
                }
                arelease(msg);
        }
}
 
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");
                arelease(msg);
                sleep(5);
        }
}
 
 
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);
}
 
DECLARE_ACTOR_MAIN(main_func)

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: http://chrismoos.com/libactordocs/

Share this post:
  • Digg
  • del.icio.us
  • Facebook
  • Reddit

14 Comments 9 Tweets

23 Comments

  1. Faried Nawaz says:

    Have you looked at http://swtch.com/libtask/ ? It’s a port of plan 9’s libthread.

  2. chrismoos says:

    Looks interesting, I’ll have to play around with it.

  3. Tony Arkles says:

    Very cool! I’ve been dabbling in Java and Scala actors. Nice to see a C port.

  4. tomdtpink says:

    checkout [libslack](http://libslack.org)

    This comment was originally posted on Reddit

  5. ipeev says:

    "Enjoying" is maybe a little harsh.

    This comment was originally posted on Reddit

  6. snarfy says:

    ew macros.

    This comment was originally posted on Reddit

  7. erez27 says:

    Interesting. Perhaps a more down-to-earth example could persuade those who don’t have experience with the actor pattern, such as myself.

    This comment was originally posted on Reddit

  8. sbrown123 says:

    Joel already said that is impossible for humans.

    This comment was originally posted on Reddit

  9. DoeL says:

    … so? They seem to be used nicely here.

    This comment was originally posted on Reddit

  10. axilmar says:

    Should the receiver free the received message?

    This comment was originally posted on Reddit

  11. case-o-nuts says:

    See also libthread on plan9: http://plan9.bell-labs.com/sources/plan9/sys/src/libthread/example.c

    This comment was originally posted on Reddit

  12. xoclipse says:

    The receiver owns the message, and should release it. But, if they don’t, when the actor exits the library cleans up the memory.

    This comment was originally posted on Reddit

  13. xoclipse says:

    Yeah..in retrospect I don’t know why I did that, it seemed nice at the time. Its just a void *(void*) #define ACTOR_FUNCTION(name, args) void *(name)(void *args)

    This comment was originally posted on Reddit

  14. self says:

    It’s [ported](http://swtch.com/libtask/) to Linux, FreeBSD, OS X, and Solaris.

    This comment was originally posted on Reddit

  15. OMouse says:

    C pre-processor macros are useful if you’re careful with them. I don’t get why you would say "ew" to them other than for the fact they’re not like Lisp macros ;)

    This comment was originally posted on Reddit

  16. netytan says:

    Some of us would say ew to Lisp macros ;)

    This comment was originally posted on Reddit

  17. snarfy says:

    My bad. I’ve done so much c++ I forget how useful and necessary they are in C.

    This comment was originally posted on Reddit

Leave a Reply

Additional comments powered by BackType