Ask Your Question
0

How to make call to multiple services in parallel?

asked 2015-10-07 09:07:24 -0500

Anshul Vij gravatar image

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); 
}
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-06-17 09:40:05 -0500

achille gravatar image

This is a while ago but this question has answers here and here

edit flag offensive delete link more
1

answered 2018-06-19 12:38:24 -0500

The link below to the multithreaded spinner is what you're looking for. Instead of spinning up only 1 thread to check and process requests, you can do N of them, where N is the input. This way multiple calls of the same service may be called at once or N different services in the same package at once.

Take care to lock resources.

http://wiki.ros.org/roscpp/Overview/C...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2015-10-07 09:07:24 -0500

Seen: 653 times

Last updated: Jun 19 '18