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

Can a single service client be used from multiple threads?

asked 2012-12-20 04:44:25 -0500

updated 2012-12-20 13:27:28 -0500

I have a use case where I have a changing number of threads needing to make service calls to the same service. I don't want to bring service clients up and down each time these threads start as they are short lived and need to start quickly, and I don't want to need to create a "service client pool" if I don't need to.

So:

  • Are ServiceClients thread-safe enough to be called from multiple threads?
  • If not, is it sufficient to mutex around the ServiceClient::call?
  • Do the answers to the previous questions depend on ROS release? (I'm stuck on electric for this project)
edit retag flag offensive close merge delete

Comments

I assume you are talking about roscpp (I have tagged your question as such). If that's incorrect, please change the tag.

Eric Perko gravatar image Eric Perko  ( 2012-12-20 10:16:20 -0500 )edit

Yes. Thanks, forgot about the distinction.

Asomerville gravatar image Asomerville  ( 2012-12-20 10:32:34 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
3

answered 2012-12-21 04:42:44 -0500

Lorenz gravatar image

updated 2012-12-21 07:18:11 -0500

If you are not using a persistent service, it does not matter if you create a new instance per call since non-persistent services always first query the core for the the service server, connect to it and drop the connection after the service call finishes. Even if you don't create a new service client, according to the source code, a new link is established for each call which should be thread safe.

With persistent services, the same socket is used for multiple calls. According to the roscpp source code, it seems like service calls are synchronized, i.e. using the same service server in multiple threads is safe, but all calls will be processed sequentially. For maximal performance, you probably should create something like a service client pool and your server should run multiple spin-threads (at least as many as you have service clients in your client pool).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-20 04:44:25 -0500

Seen: 1,042 times

Last updated: Dec 21 '12