Commit 4ab4e0f0 authored by Dominik Charousset's avatar Dominik Charousset

don't use groups in philosophers example

this patch removes spawn_in_group from the dining philosophers example
to keep the example more focused and simple
parent ce2cddae
...@@ -24,7 +24,8 @@ void chopstick() { ...@@ -24,7 +24,8 @@ void chopstick() {
send(philos, atom("taken"), self); send(philos, atom("taken"), self);
// await 'put' message and reject other 'take' messages // await 'put' message and reject other 'take' messages
become( become(
keep_behavior, // "enables" unbecome() // allows us to return to the previous behavior
keep_behavior,
on(atom("take"), arg_match) >> [=](const actor_ptr& other) { on(atom("take"), arg_match) >> [=](const actor_ptr& other) {
send(other, atom("busy"), self); send(other, atom("busy"), self);
}, },
...@@ -70,7 +71,7 @@ void chopstick() { ...@@ -70,7 +71,7 @@ void chopstick() {
* [ X = right => Y = left ] * [ X = right => Y = left ]
*/ */
struct philosopher : sb_actor<philosopher> { struct philosopher : event_based_actor {
std::string name; // the name of this philosopher std::string name; // the name of this philosopher
actor_ptr left; // left chopstick actor_ptr left; // left chopstick
...@@ -82,7 +83,6 @@ struct philosopher : sb_actor<philosopher> { ...@@ -82,7 +83,6 @@ struct philosopher : sb_actor<philosopher> {
behavior hungry; behavior hungry;
behavior denied; behavior denied;
behavior eating; behavior eating;
behavior init_state;
// wait for second chopstick // wait for second chopstick
behavior waiting_for(const actor_ptr& what) { behavior waiting_for(const actor_ptr& what) {
...@@ -151,14 +151,19 @@ struct philosopher : sb_actor<philosopher> { ...@@ -151,14 +151,19 @@ struct philosopher : sb_actor<philosopher> {
become(thinking); become(thinking);
} }
); );
}
void init() {
// philosophers start to think after receiving {think} // philosophers start to think after receiving {think}
init_state = ( become (
on(atom("think")) >> [=] { on(atom("think")) >> [=] {
aout << name << " starts to think\n"; aout << name << " starts to think\n";
delayed_send(this, seconds(5), atom("eat")); delayed_send(this, seconds(5), atom("eat"));
become(thinking); become(thinking);
} }
); );
// start thinking
send(this, atom("think"));
} }
}; };
...@@ -172,19 +177,12 @@ int main(int, char**) { ...@@ -172,19 +177,12 @@ int main(int, char**) {
aout << " " << chopsticks.back()->id(); aout << " " << chopsticks.back()->id();
} }
aout << endl; aout << endl;
// a group to address all philosophers // spawn five philosophers
auto dinner_club = group::anonymous(); std::vector<std::string> names { "Plato", "Hume", "Kant",
// spawn five philosopher, each joining the Dinner Club
std::vector<std::string> names = { "Plato", "Hume", "Kant",
"Nietzsche", "Descartes" }; "Nietzsche", "Descartes" };
for (size_t i = 0; i < 5; ++i) { for (size_t i = 0; i < 5; ++i) {
spawn_in_group<philosopher>(dinner_club, spawn<philosopher>(names[i], chopsticks[i], chopsticks[(i+1)%5]);
names[i],
chopsticks[i],
chopsticks[(i+1) % chopsticks.size()]);
} }
// tell all philosophers to start thinking
send(dinner_club, atom("think"));
// real philosophers are never done // real philosophers are never done
await_all_others_done(); await_all_others_done();
shutdown(); shutdown();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment