mardi 30 décembre 2014

Mailbox Pattern with Variable Arguments in C++


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.





Aucun commentaire:

Enregistrer un commentaire