mardi 31 mars 2020

c++, Is there any good solution for Message Sender to support send method about various type?

I want to make MessageSender to support send method about various type and satisfy followings conditions.

condition A) 'send XXX Type Message' can be added later (=Easy to expand)

condition B) each sendXTypeMessage is called in other thread (=Each method needed to be thread independent)

first my thinking was followings

class MessageSender
    {
    public:
     sendATypeMessage(ATypeData)
     sendBTypeMessage(BTypeData)
     sendCTypeMessage(CTypeData)
     ...(it can be added other type later)
    }

But in case of this, when I want to add sendDTypeMessage(data). I have to modify MessageSender class.


second my thinking was followings

class XTypeData : public IMessage
{
...
}
class MessageSender
{
public:
 sendMessage(const IMessage& data); 
}
MessageSender::sendMessage(const IMessage& data)
{
1) mutex lock
2) switch(check message type)
3) do something...
}

but in case of this, I have to use mutex lock in sendMessage.

then it can cause performance issue because each message sender thread can be blocked in sendMessage.


Is there any good solution to satisfy these conditions?

Aucun commentaire:

Enregistrer un commentaire