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.

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

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.

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật