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.
9
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();
}
}
1
I predefined all possible action types the user may trigger, and created an array of “opcodes”.
Use a struct
or class
instead of an array. Then you can store different types for each GUI action.
If you want to preserve index-based lookup, you can use a
std::tuple
instead.
Remember that an array stores a homogeneous collection of fixed size (i.e. known at compile time), whereas a struct stores a heterogeneous collection of fixed size.
The first thing that comes to my mind is to make
struct Data
{
//enter the common data here
};
struct OpcodeData: public Data
{
//enter opcode specific data here
};
and then your array[opcode].data
would be a pointer of of type Data *
actually assigned to an instance of OpcodeData *
.
But then again – do you really need to have the data in an array?
Why not have just variables opcode1Data, opcode2Data etc.?
Or even better: could the data be grouped by their meaning (physicsData, movementData, etc.) instead of by the opcode?