I'm using CAF to model a problem for my final year thesis and I've come across a scenario which I believe is not documented. In the example curl/curl_fuse.cpp
a stateful curl_worker
actor is defined and spawned as follows:
behavior curl_worker(stateful_actor<curl_state>* self, const actor& parent) {
return {
...
};
}
...
behavior curl_master(stateful_actor<master_state>* self) {
self->spawn<detached+linked>(curl_worker, self));
}
I recognize that the purpose of this approach is to isolate actors' state and prevent cyclic references, but is it possible to construct the initial curl_state
and pass it to the actor during spawn?
I know this can be done with class-based actors, however I'd want to avoid those due to aforementioned reference problem.
Yes, you can pass arbitrary arguments to
spawn
for initializing your actor like in this example:However, the CURL example stores a
self
pointer in the state. This gets tricky, because you can't initialize the state before spawning the actor in this case. The best you can do is to set the pointer outside of the constructor.Keep in mind that you need to provide a default constructor for the state regardless, because CAF creates the state before entering
my_actor
. You can of course override it immediately, but CAF still needs to construct the state initially.