C++ – Mailbox Pattern with Variable Arguments in C++

cdesign-patterns

In a game I'm developing, the GUI thread is catching user actions, and the simulation thread is responsible of handling and responding to them.

To minimize complexity and delay, I predefined all possible action types the user may trigger, and created an array of "opcodes". The GUI thread sets "happened" to the relevant array[opcode] in real time, and the simulation thread, in the proper position in code, samples array[opcode] and acts on array[opcode].data if array[opcode].happened == true.

The problem is that each opcode has a different set (size, types) of arguments. Currently I'm only allowing string arguments, and parses them in the simulation thread – not very efficient. Another solution I can think of is polymorphic opcode class and dynamically casting it in the simulation thread – ugly.

I named this the "mailbox pattern", but I'm sure someone more clever has solved (and named) it already. A pointer to a more elegant solution would be great.

Best Answer

Instead of using an array, you could use a vector<EventClass> (where EventClass is whatever type your current array is). Then you could just iterate through the vector and process each event until it's empty, assuming the GUI thread won't be adding events while the simulation thread is processing them (this could be achieved with mutexes of some kind)... You'd then only have to process active events, since the vector would only contain events that have happened. It could work like so:

class EventClass {
    EventClass(int _o, string _d) : opcode(_o), data(_d) {}
    int opcode;
    string data;
}
//somewhere available to both threads, declare the vector:
vector<EventClass> ActiveEvents;

void GUI_Process_Events(){
    //...
    ActiveEvents.push_back(The_Event_That_Just_Happened);
}

void Simulation_Process_Events(){
    //this will iterate through all the active events and
    //act on each of them
    for(int i = 0; i < ActiveEvents.size(); i++){
        //this gets the item at the back of the vector
        EventClass Current_Event = ActiveEvents.back();

        //assuming Process(EventClass e) will process the event
        Process(Current_Event);

        //this will then remove the event from the vector
        ActiveEvents.pop_back();
    }
}

If necessary, you could use a vector of pointers to EventClass objects (but then of course you must destroy them as well):

class EventClass {
    EventClass(int _o, string _d) : opcode(_o), data(_d) {}
    int opcode;
    string data;
}
//somewhere available to both threads, declare the vector:
vector<EventClass*> ActiveEvents;

void GUI_Process_Events(){
    //...
    ActiveEvents.push_back(PointerTo_The_Event_That_Just_Happened);
}

void Simulation_Process_Events(){
    //this will iterate through all the active events and
    //act on each of them
    for(int i = 0; i < ActiveEvents.size(); i++){
        //this gets the item at the back of the vector
        EventClass* Current_Event = ActiveEvents.back();

        //assuming Process(EventClass e) will process the event
        Process(Current_Event);

        //delete the object pointed to by the element in the vector
        delete Current_Event;

        //this will then remove the event from the vector
        ActiveEvents.pop_back();
    }
}
Related Topic