了解 returns void 的 CAF actor 函数

Understanding the CAF actor function which returns void

我知道演员可以通过功能来实现。以下代码片段来自 CAF github examples/hello_world.cpp.

我知道第一个实现方法,它将几个消息处理程序绑定到 actor。 actor 将在后台处于活动状态并由事件触发,然后在调用 self->quit 时终止。

但是第二个returns没什么,它的消息处理程序在哪里?而且看起来没有任何类似 self->quit 的函数可以终止自身。 hello_worldreturns的时候还活着吗?或者它在完成 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();
}

已更新,

如果行为堆栈中没有消息处理程序,Actor 将自行终止。

甚至hello_wordreturns都没有演员的倾听行为。函数 returns 时演员还活着。因为 sync_send 已经在堆栈中添加了 then-behavior 来准备来自镜像的响应。弹出一次性 then-behavior 完成响应后,堆栈为空,actor 有权自行终止。

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?

由于函数 hello_world returns void 而不是 behavior,执行此函数的 actor 在到达函数末尾后自动终止。此 actor 在生成后执行三个操作:

  1. 通过 sync_send 发送消息。
  2. 等待相应的响应。
  3. 终止。