C++ Actor Framework 0.19
Loading...
Searching...
No Matches
CAF

Introduction

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

To get started with the framework, please read the manual first.

Hello World Example

#include <iostream>
#include <string>
#include "caf/actor_ostream.hpp"
#include "caf/actor_system.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
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 std::string& what) -> std::string {
// prints "Hello World!" via aout (thread-safe cout wrapper)
aout(self) << what << std::endl;
// reply "!dlroW olleH"
return std::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 std::string& what) {
// ... and print it
aout(self) << what << std::endl;
});
}
void caf_main(actor_system& sys) {
// create a new actor that calls 'mirror()'
auto mirror_actor = sys.spawn(mirror);
// create another actor that calls 'hello_world(mirror_actor)';
sys.spawn(hello_world, mirror_actor);
// the system will wait until both actors are done before exiting the program
}
// creates a main function for us that calls our caf_main
CAF_MAIN()
Actor environment including scheduler, registry, and optional components such as a middleman.
Definition: actor_system.hpp:90
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:398
Identifies an untyped actor.
Definition: actor.hpp:29
Describes the behavior of an actor, i.e., provides a message handler and an optional timeout.
Definition: behavior.hpp:25
A cooperatively scheduled, event-based actor implementation.
Definition: event_based_actor.hpp:40
Root namespace of libcaf.
Definition: abstract_actor.hpp:29
CAF_CORE_EXPORT 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.