how to let a function wait for the completion of callback function

asked 2020-01-05 06:56:54 -0500

zhefan gravatar image

I have a class which needs to subscribe two topics, and I want to store the message into the class variable so that I can use it in another method (function) within that class. So I write c++ this: (for simplicity, I ignore some detail)

class A:
void callback1(msg1){
     this->msg1 = msg1;

void callback2(msg2){
     this-msg2 = msg2;

void myFunction(){
     doSomeThing(msg1, msg2);

int main(int argc, char** argv){

My problem is that when I call myFunction(), msg1 and msg2 have not been obtained by callback functions. So the doSomeThing(msg1, msg2) will get empty input. My question is that is there any way that myFunction can wait for completion of callback1 and callback2?

edit retag flag offensive close merge delete



Quick comment: you probably want to use message_filters instead. Especially if these are two messages with Headers. If you really don't want to use that, look at message_filters/Cache.

I would also recommend to try and search some older Q&As about this topic, as there are very many already. Use Google, and add to your search terms.

gvdhoorn gravatar image gvdhoorn  ( 2020-01-05 07:27:16 -0500 )edit

Thank you very much!

zhefan gravatar image zhefan  ( 2020-01-07 05:05:35 -0500 )edit