Ask Your Question

Multiple subscriber queue allocation using the same callback

asked 2016-03-18 01:15:02 -0500

Luis Ruiz gravatar image

updated 2016-03-18 07:20:46 -0500

I am creating multiple subscribers using the same callback function as in this post, the code is here.

Basically is this:

for ( int i = 0; i<topics.size(); i++ )  
    sub[i] = n_.subscribe( topics[i], queue_size, &MyClass::callback, this );

In this case to whom is assigned queue_size? How does it work?

I would like to think that queue_size is assigned to the subscriber sub[i] and that it doesn't matter that all the subscribers use the same callback. I mean:

Is the queue_size independent of the callback function?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-03-18 03:38:25 -0500

al-dev gravatar image

The queue_size is specific to each subscriber. It only controls the 'number of incoming messages to queue up for processing (messages in excess of this queue capacity will be discarded).' ( ).

You can have multiple subscribers using the same callback function but with different queue sizes.

e.g sub1 and sub2 both use myCallBack() and subscribe to "/mytopic1" and "/mytopic2" respectively, sub1 has queue_size=1, sub2 has queue_size=10

If I publish 15 identical messages "instantaneously" on both topics (faster than the time it takes to complete myCallBack()), you should see 2 calls to myCallBack() corresponding to sub1 for messages 1 (assuming sub1's callback is processed first) and 15, and 10 calls to myCallBack() corresponding to sub2 for messages 6-15.

Also remember that by default, all the callbacks within a node are invoked from a single global callback queue. You can assign separate callback queues for each of your subscribers if that is what you are looking for, but it takes a bit more work ( ).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2016-03-18 01:15:02 -0500

Seen: 1,077 times

Last updated: Mar 18 '16