mercredi 1 avril 2020

c++, could you help me my 'MessageSender class' issue? (Each method of MessageSender should work for independent threads)

class IMessage{
    public:
    virtual void send(const MessageSender&)  = 0;
};

class ATypeData : public IMessage
{
    public:
    void send(const MessageSender& s) ; 
};

class BTypeData : public IMessage {
    public:
    void send(const MessageSender& s) ; 
};

class CTypeData : public IMessage {
    public:
    void send(const MessageSender& s) ; 
};

class MessageSender
{
public:
     sendMessage(IMessage& data) { data.send(*this); }
     sendATypeMessage(ATypeData&) const { mutex msgALock; do something A; }
     sendBTypeMessage(BTypeData&) const { mutex msgBLock; do something B; }
     sendCTypeMessage(CTypeData&) const { mutex msgCLock; do something C; }
     ...(it can be added other type later)
}
...
threadA() { while(1) { sendATypeMessage(ATypeData); }
threadB() { while(1) { sendBTypeMessage(BTypeData); }
threadC() { while(1) { sendCTypeMessage(CTypeData); }
...
main() { start threadA(); start threadB(); start threadC() ... }

In case of this, If 'do something A' is not finish,

sendBTypeMessage, sendCTypeMessage are delayed while 'do something A' is finished.

I want to modify it that sendXTypeMessage can process regardless of sendATypeMessage inner behavior.

Is there any good solution? or my requirement is impossible?

Aucun commentaire:

Enregistrer un commentaire