How to make sure all messages are published before calling ros::Publisher::shutdown?
In my C++ program I publish a message to a ros::Publisher with the publish method and then immediately shuts down the publisher using the shutdown method (because the program is finished). However, the result is that the message is not published. If I add a sleep of a few milliseconds before the shutdown, then it works. My question is, how do I make sure that all messages waiting in the publisher's queue are sent before calling shutdown?
I tried explicitly calling the processPublishQueue on the publication associated with the topic after publish and before shutdown: ros::PublicationPtr pub = ros::TopicManager::instance()->lookupPublication(topic_name); if (pub) { pub->processPublishQueue(); } But it didn't send the message.
Adding a cout statement or sleep(1000) however works fine, but it seems very risky to rely on this to make sure the message is actually sent.