for(;;) { // The main state.
receive(
when([](M1 msg1){ // Dive into another state.
while(some_condition) {
receive(when([](M2 msg2){...},
when([](M7 msg7){...},
...);
}
}),
when([](M3 msg3){...}),
when([](M5 msg5){...}),
...);
}
In that case, if you receive M2 before M1 the instance of M2 will be kept in the queue.But if you have to write something like that:
for(;;) { // The main state.
auto * m = receive();
if(m->type == M1::msg_typeid) { // Dive into another state.
while(some_condition) {
auto * m2 = receive();
if(m2->type == M2::msg_typeid) {...}
else if(m2->type == M7::msg_typeid){...}
else if...
}
}
else if(m->type == M3::msg_typeid){...}
else if(m->type == M5::msg_typeid){...}
else if...
}
then you can easily lose a message by a mistake.