The actor model organizes concurrent work into independent actors that communicate by sending messages rather than sharing state directly. section 4.4.2
An ATM state machine
An ATM actor handles messages such as card insertion and PIN digits, updates its state, and sends messages to separate bank and interface actors. Its logic focuses on which messages to accept and send, rather than managing synchronization. section 4.4.2
Listing 4.15. A simple implementation of an ATM logic class
struct card_inserted
{
std::string account;
};
class atm
{
messaging::receiver incoming;
messaging::sender bank;
messaging::sender interface_hardware;
void (atm::*state)();
std::string account;
std::string pin;
void waiting_for_card() #1
{
interface_hardware.send(display_enter_card()); #2
incoming.wait() #3
.handle<card_inserted>(
[&](card_inserted const& msg) #4
{
account=msg.account;
pin="";
interface_hardware.send(display_enter_pin());
state=&atm::getting_pin;
}
);
}
void getting_pin();
public:
void run() #5
{
state=&atm::waiting_for_card; #6
try
{
for(;;)
{
(this->*state)(); #7
}
}
catch(messaging::close_queue const&)
{
}
}
};
!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":12,\"ch\":57},{\"line\":12,\"ch\":59}],[{\"line\":14,\"ch\":57},{\"line\":14,\"ch\":59}],[{\"line\":15,\"ch\":57},{\"line\":15,\"ch\":59}],[{\"line\":17,\"ch\":57},{\"line\":17,\"ch\":59}],[{\"line\":28,\"ch\":57},{\"line\":28,\"ch\":59}],[{\"line\":30,\"ch\":57},{\"line\":30,\"ch\":59}],[{\"line\":35,\"ch\":57},{\"line\":35,\"ch\":59}]]"}
!@%STYLE%@!
See the C++ ATM logic class, including message handling and state transitions. section 4.4.2

