C++ 在C++;参与者框架?
我正试图把一些作品从一个打字演员交给另一个演员。CAF用户手册指出,可以使用C++ 在C++;参与者框架?,c++,c++-actor-framework,C++,C++ Actor Framework,我正试图把一些作品从一个打字演员交给另一个演员。CAF用户手册指出,可以使用forward\u to方法进行此操作。该方法似乎只适用于显式为基于事件的参与者类型的参与者。但是,forward\u to似乎是forward\u current\u message方法上的一个薄包装,该方法是为local\u actor类型的所有参与者定义的。因此,我假设可以直接调用forward\u current\u message 此外,为了使消息转发与类型化的参与者一起工作,我仍然需要返回中间参与者的响应。那
forward\u to
方法进行此操作。该方法似乎只适用于显式为基于事件的参与者
类型的参与者。但是,forward\u to
似乎是forward\u current\u message
方法上的一个薄包装,该方法是为local\u actor
类型的所有参与者定义的。因此,我假设可以直接调用forward\u current\u message
此外,为了使消息转发与类型化的参与者一起工作,我仍然需要返回中间参与者的响应。那个演员的反应似乎被忽视了,这是好的,但我做错了什么吗?还是真的有必要为构建一个不被使用的响应支付(通常是最低的)成本
下面是一些工作示例代码,演示了我使用键入的参与者进行消息转发的尝试:
#include <iostream>
#include "caf/all.hpp"
using namespace caf;
using namespace std;
using a_type = typed_actor<replies_to<int>::with<bool>>;
using b_type = typed_actor<replies_to<int>::with<bool>>;
actor worker()
{
return spawn(
[](event_based_actor *self) -> behavior
{
return
{
[self](int index)
{
aout(self) << "Worker: " << index << endl;
return index;
}
};
});
}
b_type::behavior_type bBehavior(b_type::pointer self)
{
return
{
[self](int value)
{
// Create blocking actor
scoped_actor blockingActor;
// Spawn pool workers and send each a message
auto pool = actor_pool::make(value, worker, actor_pool::round_robin());
for(int i = 0; i < value; ++i)
{
blockingActor->send(pool, i);
}
// Wait for completion
vector<int> results;
int i = 0;
blockingActor->receive_for(i, value) (
[&results](int value)
{
results.push_back(value);
});
blockingActor->send_exit(pool, exit_reason::user_shutdown);
self->quit();
return (value == results.size());
}
};
}
class A : public a_type::base
{
protected:
behavior_type make_behavior() override
{
return
{
[this](int value) -> bool
{
aout(this) << "Number of tasks: " << value << endl;
b_type forwardDestination = spawn(bBehavior);
auto castDestination = actor_cast<actor>(forwardDestination);
this->forward_current_message(castDestination);
this->quit();
return false;
}
};
}
};
void tester()
{
a_type testeeActor = spawn<A>();
scoped_actor self;
self->sync_send(testeeActor, 5).await(
[testeeActor, &self](bool success)
{
aout(self) << "All workers completed? " << (success ? "Yes!" : "No :(") << endl;
});
}
int main()
{
tester();
await_all_actors_done();
shutdown();
cout << "Press Enter to continue" << endl;
cin.get();
}
#包括
#包括“caf/all.hpp”
使用名称空间caf;
使用名称空间std;
使用类型=类型化的参与者;
使用b_类型=类型化的参与者;
演员工人()
{
返回产卵(
[](基于事件的参与者*自身)->行为
{
返回
{
[self](整数索引)
{
aout(自)发送\退出(池,退出\原因::用户\关闭);
自我->退出();
返回值(value==results.size());
}
};
}
A类:公共A_类型::基本
{
受保护的:
行为类型使行为()覆盖
{
返回
{
[this](int值)->bool
{
A输出(此)同步发送(测试仪,5)。等待(
[测试人员和自我](bool success)
{
aout(自我)
因此,我假设可以直接调用当前消息
否,forward\u current\u message
不是CAF中公共API的一部分(因此未在中列出)。这意味着可以随时重命名、删除或使成员函数受保护
/私有
将消息转发给类型化参与者的最佳实践是委托
。这是一项新功能(随介绍),遗憾的是手册中没有提到。当前可用的最佳“文档”是在中使用它
简短的版本是:delegate
是转发请求责任的send
的替代方法。在键入的参与者中,您可以从消息处理程序返回delegate
,而不是T
,以指示其他参与者将使用T
响应原始发件人
在您的情况下,类A
的实现方式如下:
class A : public a_type::base
{
protected:
behavior_type make_behavior() override {
return {
[this](int value) {
aout(this) << "Number of tasks: " << value << endl;
auto forwardDestination = spawn(bBehavior);
this->quit();
return delegate(forwardDestination, value);
}
};
}
};
A类:公共A_类型::基本
{
受保护的:
行为类型使行为()覆盖{
返回{
[此](整数值){
aout(这)谢谢!这更加优雅-它消除了强制转换和创建默认值的需要。对于其他遇到这一问题的人,我想澄清一下,尽管处理程序的返回类型发生了变化,但在使用委托时,类型化的_actor模板定义保持不变。