Subscribers not getting messages in the right queue, multithreading.

asked 2016-09-28 08:03:05 -0500

Hakaishin gravatar image

updated 2016-10-14 08:55:31 -0500

I am trying to do some multithreading. I set up 3 threads and every thread has it's own callbackqueue and I try to call it by cb.callAvailable(); This works, but the subscribers don't get messages or atleast the callbacks from the subscribers never get called.

Main.cpp

std::vector<SingleAgentSystem> quads;
ros::CallbackQueue cbs[kNumQuads];
for (int i = 0; i < kNumQuads; i++)
{
    quads.emplace_back("quad" + i + 1, &cbs[i]);
    threads[i] = std::thread(std::ref(quads[i]), &cbs[i], i);
}

SingleAgentSystem.h

void callRos(ros::CallbackQueue* cb, int i);
void operator()(ros::CallbackQueue* cb, int i)
{
  callRos(cb, i);
}

SingleAgentSystem.cpp in the constructor

SingleAgentSystem::SingleAgentSystem(const std::string& namespace_name, ros::CallbackQueue* cb)
    {  
nh_ = ros::NodeHandle(namespace_name);
nh_.setCallbackQueue(cb);
image_transport::ImageTransport it(nh_);
image_subscriber_ = it.subscribe("camera/image_raw",1,&SingleAgentSystem::transformImageCallback, this);
pose_subscriber_ = nh_.subscribe("position",1,&SingleAgentSystem::getPositionCallback, this);
}

void SingleAgentSystem::callRos(ros::CallbackQueue* cb, int i)
{
  while (ros::ok())
  {
    std::cout << "Calling from thread" + std::to_string(i) << std::endl;
    cb->callAvailable();
  }
}

Callbacks

void SingleAgentSystem::transformImageCallback(
    const sensor_msgs::ImageConstPtr& msg)
{
  std::cout << "Received Image/Position" << std::endl;
...
}

It is strange, the calling from thread i is getting printed but no callback is ever called. I don't really know what I'm doing wrong. Any ideas? }

EDIT: Multiple Callbackqueues were not really needed could solve my problem with the AsynSpinners.

edit retag flag offensive close merge delete