Commit 6be5c76b authored by Dominik Charousset's avatar Dominik Charousset

Use `behavior::assign` to initialize philosophers

parent 90b8333f
...@@ -99,24 +99,24 @@ class philosopher : public event_based_actor { ...@@ -99,24 +99,24 @@ class philosopher : public event_based_actor {
left(l), left(l),
right(r) { right(r) {
// a philosopher that receives {eat} stops thinking and becomes hungry // a philosopher that receives {eat} stops thinking and becomes hungry
thinking = behavior{ thinking.assign(
[=](eat_atom) { [=](eat_atom) {
become(hungry); become(hungry);
send(left, take_atom::value); send(left, take_atom::value);
send(right, take_atom::value); send(right, take_atom::value);
} }
}; );
// wait for the first answer of a chopstick // wait for the first answer of a chopstick
hungry = behavior{ hungry.assign(
[=](taken_atom) { [=](taken_atom) {
become(granted); become(granted);
}, },
[=](busy_atom) { [=](busy_atom) {
become(denied); become(denied);
} }
}; );
// philosopher was able to obtain the first chopstick // philosopher was able to obtain the first chopstick
granted = behavior{ granted.assign(
[=](taken_atom) { [=](taken_atom) {
aout(this) << name aout(this) << name
<< " has picked up chopsticks with IDs " << " has picked up chopsticks with IDs "
...@@ -131,9 +131,9 @@ class philosopher : public event_based_actor { ...@@ -131,9 +131,9 @@ class philosopher : public event_based_actor {
send(this, eat_atom::value); send(this, eat_atom::value);
become(thinking); become(thinking);
} }
}; );
// philosopher was *not* able to obtain the first chopstick // philosopher was *not* able to obtain the first chopstick
denied = behavior{ denied.assign(
[=](taken_atom) { [=](taken_atom) {
send(last_sender() == left ? left : right, put_atom::value); send(last_sender() == left ? left : right, put_atom::value);
send(this, eat_atom::value); send(this, eat_atom::value);
...@@ -143,9 +143,9 @@ class philosopher : public event_based_actor { ...@@ -143,9 +143,9 @@ class philosopher : public event_based_actor {
send(this, eat_atom::value); send(this, eat_atom::value);
become(thinking); become(thinking);
} }
}; );
// philosopher obtained both chopstick and eats (for five seconds) // philosopher obtained both chopstick and eats (for five seconds)
eating = behavior{ eating.assign(
[=](think_atom) { [=](think_atom) {
send(left, put_atom::value); send(left, put_atom::value);
send(right, put_atom::value); send(right, put_atom::value);
...@@ -153,7 +153,7 @@ class philosopher : public event_based_actor { ...@@ -153,7 +153,7 @@ class philosopher : public event_based_actor {
aout(this) << name << " puts down his chopsticks and starts to think\n"; aout(this) << name << " puts down his chopsticks and starts to think\n";
become(thinking); become(thinking);
} }
}; );
} }
protected: protected:
......
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