spin with threads
Hi. I'm having problems with spin and multiples subscriptions. I want to subscribe to two topic and going on with my program. Something like:
X_callback(){something};
Y_callback(){something};
int main(){
thread(subscribe to topic X with callback X_callback)
thread(subscribe to topic Y_callback)
more things to do...
}
I don't know what to do with spin() if I've threads. I've used Multithread spin but doesn't work. Doing something like:
MultiThreadSping s(4);
X_callback(){s.spin()};
Y_callback(){s.spin()};
int main(){
some code
}
EDIT. This was my first idea:
void alivesCallback(const std_msgs::String::ConstPtr &msg){
//control new connection
}
void temperatureCallback(const std_msgs::String::ConstPtr &msg){
temperature = msg->data;
}
void subscriptionAliveTopic(){
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("alive", 1000, alivesCallback);
ros::spin();
}
void subscriptionTemperatureTopic(){
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("temperature", 1000, temperatureCallback);
ros::spin();
}
int main(){
boos::thread t1(boost::bind(subscriptionAliveTopic));
boos::thread t2(boost::bind(subscriptionTemperatureTopic));
//more things to do
}
Can you clarify your question a bit? Do you just want to subscribe to two topics? Is there a reason that you need threads, other than for the subscriptions?
Each thread is the subscription and the spin. I'm using threads beacuse I'm trying not to block the main program.