liveBook · Manning

Actor model

As explained in C++ Concurrency in Action, Second Edition

The actor model is a concurrency model in which independent actors communicate by sending messages and do not share state directly.

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

How C++ Concurrency in Action, Second Edition covers it

How deep it goes
Defines actors through a simplified ATM example, tracing message handling and state changes while leaving realistic ATM logic aside.
Examples
Demonstrates an ATM state machine that handles card, PIN, and button messages and communicates with bank and interface components.
What you can do after
Understand how to organize concurrent components as message-driven actors and reason about accepted messages and state transitions.
How it connects
Connects message passing with threads, state machines, message handlers, and shared state.

Also explained in

Compare how 2 books explain actor model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of C++ Concurrency in Action, Second Edition. The section links point to the parts of the book it draws on.