ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Callback function is executed more than one time

asked 2022-08-22 13:20:16 -0600

hck007 gravatar image

I'm learning multithreaded-spinning on the website. I'm confused why the second callback function is invoked two times after the previous callback is stuck for a few seconds. In my understanding, each invoke of callback function only processes one message in the queue. Why does one execution of callback function in this case can process two message?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-08-22 16:14:50 -0600

Mike Scheutzow gravatar image

updated 2022-08-22 16:16:18 -0600

You have misunderstood. Messages start their processing in the order they are queued, but when a thread finishes its current work, it is re-assigned to execute the callback for the next queued message. Depending on the message type, it may execute the same callback it just finished with, or it may execute a different one.

The code you link to passes an argument of 0 to the AsyncSpinner constructor. That causes it to allocate a thread for each cpu core on the current machine. For example, if your machine has 4 cores, then AsyncSpinner may execute up to 4 subscriber callbacks simultaneously.

edit flag offensive delete link more

Comments

Thanks for your answer! Do you mean two kinds of messages from two different publishers are pushed into the same global callback queue in the order of arrival date, and then each message is processed by its' corresponding callback function. Thus, it's very likely that we may have two adjacent messages from the same publisher.

hck007 gravatar image hck007  ( 2022-08-22 16:31:38 -0600 )edit

Yes. I have not looked at the actual implementation, but incoming messages are effectively queued in the order of arrival (which I assume means the arrival of the last byte of the message.)

Mike Scheutzow gravatar image Mike Scheutzow  ( 2022-08-22 20:04:48 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2022-08-22 13:20:16 -0600

Seen: 111 times

Last updated: Aug 22 '22