g++ -std=c++11 -finstrument-functions -fpermissive -g -I\usr\local\include -o transform transform.cpp -L\usr\local\lib -lcaf_io -lcaf_core
transform.cpp: In lambda function:
transform.cpp:37:19: warning: passing ‘const Transform’ as ‘this’ argument of ‘Transform& Transform::operator=(const Transform&)’ discards qualifiers [-fpermissive]
transform = t;
^
#include <string>
#include <iostream>
#include "caf/all.hpp"
using std::endl;
using std::string;
using namespace caf;
struct Transform
{
float x, y, z;
};
behavior gravity(event_based_actor* self) {
// return the (initial) actor behavior
return {
[=](Transform transform) -> Transform {
transform.y -= 9.8; // m/s
self->quit();
return transform;
}
};
}
void gravitySystem(event_based_actor* self) {
Transform transform;
transform.x = 0.0f;
transform.y = 0.0f;
transform.z = 0.0f;
for (int i = 0; i < 10; i++) {
auto gravity_actor = spawn(gravity);
self->sync_send(gravity_actor, transform).then(
[=](Transform t) {
aout(self) << "X: " << t.x << " Y: " << t.y << " Z: " << t.z << endl;
transform = t;
}
);
}
}
int main() {
spawn(gravitySystem);
await_all_actors_done();
shutdown();
}
[=](Transform t) {
aout(self) << "X: " << t.x << " Y: " << t.y << " Z: " << t.z << endl;
transform = t;
}
[&](Transform t) {
aout(self) << "X: " << t.x << " Y: " << t.y << " Z: " << t.z << endl;
transform = t;
}
I guess
[=](Transform t) {
aout(self) << "X: " << t.x << " Y: " << t.y << " Z: " << t.z << endl;
transform = t;
}
should be
[&](Transform t) {
aout(self) << "X: " << t.x << " Y: " << t.y << " Z: " << t.z << endl;
transform = t;
}
After changing `[=]` to `[&]` (...) the program is still flawed.
Doesn't look like stateful_actors contain a request method, bummer.
Now I'm having a problem that might be attributed to that but I'm not sure if that's it and if it is that what to do about it. See "transform.out" and "transform.cpp". Somehow my FOR loop is sending all 10 actors to the behaviors twice (total of 20) before the first for loop completes. Everything is out of order, which is probably why the same Transform is -9.8 twice in a row instead of -9.8 then -19.6 the second time.
--
You received this message because you are subscribed to the Google Groups "actor-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actor-framewo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<transform.out><transform.cpp>
The second method you described sounds kind of close, but I think that essentially causes serial execution where a worker has to wait until the current Transform is finished updating before starting on the next one. Unless I'm wrong?
Thank you for all the help, by the way, this has been immensely helpful and informative.
Hi. To get both Transform and ID back from my worker, do I need to use a tuple? Is that implied here or is [&](Transform t, int id) really possible?