Loading...
Searching...
No Matches
CAF

# Introduction

This library provides an implementation of the actor model for C++. It uses a network transparent messaging system to ease development of both concurrent and distributed software.

libcaf uses a thread pool to schedule actors by default. A scheduled actor should not call blocking functions. Individual actors can be spawned (created) with a special flag to run in an own thread if one needs to make use of blocking APIs.

Writing applications in libcaf requires a minimum of gluecode and each context is an actor. Scoped actors allow actor interaction from the context of threads such as main.

# Getting Started

To build libcaf, you need GCC >= 4.8 or <tt>Clang >= 3.2</tt>, and CMake`.

The usual build steps on Linux and macOS are:

- ./configure
- make

- `make install (as root, optionally)

  Please run the unit tests as well to verify that libcaf works properly.

- `make test

  Please submit a bug report that includes (a) your compiler version, (b) your OS, and (c) the output of the unit tests if an error occurs: https://github.com/actor-framework/actor-framework/issues

  Please read the Manual for an introduction to libcaf. It is available online on Read The Docs at https://actor-framework.readthedocs.io or as PDF at http://www.actor-framework.org/pdf/manual.pdf

# Hello World Example

#include <string>
#include <iostream>
#include "caf/all.hpp"
using std::endl;
using std::string;
using namespace caf;
behavior mirror(event_based_actor* self) {
// return the (initial) actor behavior
return {
// a handler for messages containing a single string
// that replies with a string
[=](const string& what) -> string {
// prints "Hello World!" via aout (thread-safe cout wrapper)
aout(self) << what << endl;
// reply "!dlroW olleH"
return string(what.rbegin(), what.rend());
}
};
}
void hello_world(event_based_actor* self, const actor& buddy) {
// send "Hello World!" to our buddy ...
self->request(buddy, std::chrono::seconds(10), "Hello World!").then(
// ... wait up to 10s for a response ...
[=](const string& what) {
// ... and print it
aout(self) << what << endl;
}
);
}
void caf_main(actor_system& system) {
// create a new actor that calls 'mirror()'
auto mirror_actor = system.spawn(mirror);
// create another actor that calls 'hello_world(mirror_actor)';
system.spawn(hello_world, mirror_actor);
// system will wait until both actors are destroyed before leaving main
}
// creates a main function for us that calls our caf_main
CAF_MAIN()
caf::actor_system
Actor environment including scheduler, registry, and optional components such as a middleman.
Definition actor_system.hpp:117
caf::actor_system::spawn
infer_handle_from_class_t< C > spawn(Ts &&... xs)
Returns a new actor of type C using xs... as constructor arguments.
Definition actor_system.hpp:321
caf::actor
Identifies an untyped actor.
Definition actor.hpp:42
caf::behavior
Describes the behavior of an actor, i.e., provides a message handler and an optional timeout.
Definition behavior.hpp:39
caf::event_based_actor
A cooperatively scheduled, event-based actor implementation.
Definition event_based_actor.hpp:53
caf
Root namespace of libcaf.
Definition abstract_actor.hpp:44
caf::aout
actor_ostream aout(local_actor *self)
Convenience factory function for creating an actor output stream.

# More Examples

The `Math Actor Example` shows the usage of `receive_loop` and `arg_match`. The `Dining Philosophers Example` introduces event-based actors covers various features of CAF.