【问题标题】:Why CAF system.registry() should call erase(atom_value) automatically after put(atom_value)为什么 CAF system.registry() 应该在 put(atom_value) 之后自动调用 erase(atom_value)
【发布时间】:2019-01-03 15:56:50
【问题描述】:

当我通过调用 put(atom_value, actor ),我的进度不会自动终止,直到我杀死它才会停止。

但是,当我显式添加 erase() 时,它将退出。

其实正如CAF手册第8节所说:

Actor 终止时会自动移除。

这就是我想要的。所以我想知道这是否是一个错误。

  • 我在 MacOS 10.13.4 上运行,
  • 编译器是 Apple LLVM 版本 9.1.0 (clang-902.0.39.2)。
  • CAF 版本:0.15.7

为了重现我的结果,请运行以下 c++ 代码:

#include <string>
#include <iostream>
#include "caf/all.hpp"
#include "caf/io/all.hpp"


using std::endl;
using std::string;
using namespace caf;

atom_value world_atom = atom("world_act");
atom_value hello_atom = atom("hello_act");

behavior world(event_based_actor *self) {

  return behavior {
    [=](const string &what) {
      aout(self) << "Message: " << what << endl;
      return std::string{"World"};
    }
  };
}

void hello(event_based_actor* self) {

  string out_msg{"Hello "};
  auto tmp = self->home_system().registry().get(world_atom);
  auto buddy = actor_cast<actor>(tmp);

  self->request(actor_cast<actor>(tmp), std::chrono::seconds(2), out_msg).then(
    [=] (const string& what) {
      aout(self) << "Message: " << what << endl;
    });
}

int main() {
  actor_system_config cfg;
  actor_system system{cfg};

  auto world_actor = system.spawn(world);
  system.registry().put(world_atom, world_actor);

  auto hello_actor = system.spawn(hello);
  system.registry().put(hello_atom, hello_actor);

  std::cout << "current running: " << system.registry().running() << endl;

  /* Uncomment the following three lines if you want to stop automatically */
//  getchar();
//  system.registry().erase(hello_atom);
//  system.registry().erase(world_atom);

  return 0;

}

【问题讨论】:

    标签: c++ actor c++-actor-framework


    【解决方案1】:

    您的进程不会终止,因为 CAF 的演员系统在关闭之前会等待所有演员。通常的 CAF 应用程序使用 main 仅用于启动演员。

    Actor 终止时会自动删除。

    这就是我想要的。

    当您从示例中的注册表中删除演员时,他们将变得无法访问并因此终止。一旦你的演员全部终止,演员系统就会关闭。但是,您的 sn-p 永远不会明确终止 world 演员,即它永远不会调用 self-&gt;quit()。你可以通过anon_send_exit(world_actor, exit_reason::user_shutdown) 强制它。如手册中所述,终止它也会将其从注册表中删除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-15
      • 2023-03-28
      • 2020-09-06
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2017-12-01
      • 2011-03-21
      相关资源
      最近更新 更多