Ask Your Question

Revision history [back]

I know this question has been answered by @joq, but that was quite some time ago.

Not sure when it was added, but I believe that ros::topic::waitForMessage(..) (api docs) is a much more elegant solution when only needing a single message.

This function takes care of everything that @joq suggests, ie: subscribing, waiting for a message and then unsubcribing immediately. The caller does not need to maintain any state him or herself (ie: no flag handling, no additional callbacks to ignore).

See #q270298 for an example that shows how to use ros::topic::waitForMessage(..).