I know actors can be implemented by function. The following code snippet is from CAF github examples/hello_world.cpp.
I know the first one implementation method, which binds a couple of message handlers to the actor. The actor will be alive at background and triggered by events, then terminated when self->quit
is called.
But the second one returns nothing, where is its message handler? And it looks like no any self->quit
-like function to terminate itself. Is it alive when hello_world returns? Or it just terminate itself after finished the response in then
?
behavior mirror(event_based_actor* self) {
return {
[=](const string& what) { ... }
[=](int) { ...}
}
};
void hello_world(event_based_actor* self, const actor& buddy) {
self->sync_send(...).then(
...
);
}
int main() {
auto mirror_actor = spawn(mirror);
spawn(hello_world, mirror_actor);
await_all_actors_done();
shutdown();
}
Updated,
Actor will terminate itself if non of message handlers in its behavior stack.
Even hello_word
returns nothing as actor's listening behavior. The actor is still alive when function returns. Because the sync_send
has added the then-behavior
in stack to prepare the responding from mirror. After the one-shot then-behavior
is popped up to finish the response, stack is empty then actor is right to terminate itself.
But the second one returns nothing, where is its message handler? And it looks like no any
self->quit
-like function to terminate itself. Is it alive whenhello_world
returns? Or it just terminate itself after finished the response in then?
Since the function hello_world
returns void and not behavior
, the actor executing this function automatically terminates once it reaches the end of the function. This actor performs three actions after being spawned:
sync_send
.