How to make call to multiple services in parallel?
I have 4 services which have are completely independent of each other. I want to call them in parallel from another ros node.
But the all service call end consuming the time that they would use in sequence. Is there any way to call services parallelly or this is a ROS limitation?
Please have a look at my code for VC++
void service1Thread(void *param) {
printf(currentDateTime()+"Started service1Thread\n").c_str());
float output =service1Client->compute(var1);
printf((currentDateTime()+"Ended service1Thread\n").c_str());
_endthread();
}
void service2Thread(void *param) {
printf(currentDateTime()+"Started service2Thread\n").c_str());
float output =service2Client->compute(var2);
printf((currentDateTime()+"Ended service2Thread\n").c_str());
_endthread();
}
void service3Thread(void *param) {
printf(currentDateTime()+"Started service3Thread\n").c_str());
float output =service3Client->compute(var3);
printf((currentDateTime()+"Ended service3Thread\n").c_str());
_endthread();
}
void service4Thread(void *param) {
printf(currentDateTime()+"Started service4Thread\n").c_str());
float output =service4Client->compute(var4);
printf((currentDateTime()+"Ended service4Thread\n").c_str());
_endthread();
}
int main(args[]) {
printf((currentDateTime()+"Calling thread service1Thread\n").c_str());
HANDLE handle1 = (HANDLE)_beginthread( service1Thread, 0, NULL);
printf((currentDateTime()+"Calling thread service2Thread\n").c_str());
HANDLE handle2= (HANDLE)_beginthread( service2Thread, 0, NULL);
printf((currentDateTime()+"Calling thread service3Thread\n").c_str());
HANDLE handle3 = (HANDLE)_beginthread( service3Thread, 0, NULL);
printf((currentDateTime()+"Calling thread service4Thread\n").c_str());
HANDLE handle4 = (HANDLE)_beginthread( service4Thread, 0, NULL);
printf((currentDateTime()+"Waiting for all threads\n").c_str());
HANDLE lpHandles[] = {handle1 ,handle2 ,handle3 ,handle4 };
WaitForMultipleObjects(4,lpHandles,true,INFINITE);
printf((currentDateTime()+"Finished waiting for all threads\n").c_str());
}
const std::string currentDateTime() {
SYSTEMTIME st, lt;
GetSystemTime(&st);
char currentTime[84] = "";
sprintf(currentTime,"[%d:%d:%d.%d] ", st.wHour, st.wMinute, st.wSecond , st.wMilliseconds);
return string(currentTime);
}