Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Is It Safe to Subscribe Multiple Times to the Same Topic?

I would like to know whether it is a safe practice to subscribe multiple times (within the same C++ node) to the same topic. The purpose of this, is to assign two different callbacks to the reception of a single message.

My use case does not allow me to write all the code within a single callback (one of the callbacks is provided by external code), so I see one of two options.

  1. Use bindings (e.g. boost::bind) to compose the two callbacks. This involves complex type management, because I want a MessageType::ConstPtr, but I do not know in advance what the other callback will require (might just be a reference).
  2. Assign multiple callbacks to the same topic. This option is a lot easier to manage, but I am unsure whether it is safe, recommended, or whether it incurs a significant performance penalty.

I have already tried a simple subscriber that subscribes twice to the same topic, using different callbacks, so at least I know that it seems to work. I do not know whether there is some recommendation in favour or against this practice. Is it prone to some specific kind of bug? Does it have to create copies of the message?