Overview¶
Compiling CAF requires CMake and a recent C++ compiler. To get and compile the sources on UNIX-like systems, type the following in a terminal:
git clone https://github.com/actor-framework/actor-framework
cd actor-framework
./configure
make -C build
make -C build install [as root, optional]
Running configure
is not a mandatory step. The script merely automates the
CMake setup and makes setting build options slightly more convenient. On
Windows, use CMake directly to generate an MSVC project file.
Features¶
Lightweight, fast and efficient actor implementations
Network transparent messaging
Error handling based on Erlang’s failure model
Pattern matching for messages as internal DSL to ease development
Thread-mapped actors for soft migration of existing applications
Publish/subscribe group communication
Supported Operating Systems¶
Linux
Windows
macOS
FreeBSD
Hello World Example¶
// The obligatory "Hello World!" example.
#include "caf/actor_ostream.hpp"
#include "caf/actor_system.hpp"
#include "caf/caf_main.hpp"
#include "caf/event_based_actor.hpp"
#include <string>
using namespace caf;
using namespace std::literals;
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
[self](const std::string& what) -> std::string {
// prints "Hello World!" (thread-safe)
self->println("{}", what);
// 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->mail("Hello World!")
.request(buddy, 10s)
.then(
// ... wait up to 10s for a response ...
[self](const std::string& what) {
// ... and print it
self->println("{}", what);
});
}
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()