mardi 28 février 2017

c++ lambda callback to trigger event

I have been trying to wrap my head around the callback functionality in c++. What I am trying to achieve is the following:

I have two objects, each one with its own thread. One object A has a pointer to the second object B. See example:

class A
{
  public:
   // ...
  private:
   std::unique_ptr<B> b;
};

class B
{
  public:
   void add_message(MessageType msg);
   // ...
};

What I am trying to achieve is having object A add a message using the pointer to B and then continue doing other stuff, but having a callback or handler or something that gets triggered when B has a reply to that message. B does some processing with the message and might pass it to other objects for processing on its own thread but eventually will come up with a reply. So how can I know when B has a reply to my message, for example:

// In class A
MessageType m();
b->add_message(m)
// A's thread continues doing other stuff
...
// some notification that b has a reply?

I know I might have to use std::function for a callback which I would like to use but I can't get my head around how exactly to do this by looking at a lot of examples already. Any help is appreciated and note that I have looked at a lot of examples but can't tie it back to what I am trying to achieve or am not understanding...

Aucun commentaire:

Enregistrer un commentaire