Gsoap Multithreading

gSOAP Multithreading

Important:

This code requires gsoap version 2.8.5 as a minimum. It was initially built on Solaris 8 with gsoap version 2.8.3, porting the code to Ubuntu and running under valgrind showed that the 2.8.3 gsoap++ library was corrupting memory which lead to a SIGSEGV. It should be noted that as of 25/11/11 the version of gsoap that Ubuntu installs using apt-get is the broken 2.8.3. A manual download and build of the latest version of gsoap was required (make sure to install flex and bison before you configure the gsoap build!).

Using gsoap 2.8.5 the code below happily creates threads and serves SOAP messages to multiple clients, valgrind now reports 0 errors with the memory allocation.


Looking at your code the example you have working has been built with the -i (or -j) option to create C++ objects. The thread examples in the gsoap doumention are written in standard C; hence the reference to functions such as soap_serve() which you don't have.

Below is my quick re-write of the multithreaded example to use the C+ objects generated. It is based on the following definition file:

// Content of file "calc.h": 
//gsoap ns service name: Calculator
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service location: http://www.cs.fsu.edu/~engelen/calc.cgi
//gsoap ns schema namespace: urn:calc
//gsoap ns service method-action: add ""
int ns__add(double a, double b, double &result);
int ns__sub(double a, double b, double &result);
int ns__mul(double a, double b, double &result);
int ns__div(double a, double b, double &result);

The main server code then looks like this:

#include "soapCalculatorService.h"  // get server object 
#include "Calculator.nsmap" // get namespace bindings

#include <pthread.h>

void *process_request(void *calc) ;

int main(int argc, char* argv[])
{
CalculatorService c;
int port = atoi(argv[1]) ;
printf("Starting to listen on port %d\n", port) ;
if (soap_valid_socket(c.bind(NULL, port, 100)))
{
CalculatorService *tc ;
pthread_t tid;
for (;;)
{
if (!soap_valid_socket(c.accept()))
return c.error;
tc = c.copy() ; // make a safe copy
if (tc == NULL)
break;
pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)tc);

printf("Created a new thread %ld\n", tid) ;
}
}
else {
return c.error;
}

}

void *process_request(void *calc)
{
pthread_detach(pthread_self());
CalculatorService *c = static_cast<CalculatorService*>(calc) ;
c->serve() ;
c->destroy() ;
delete c ;
return NULL;
}

This is a very basic threading model but it shows how to use the C++ classes generated by gsoap to build a multithreaded server.

Multiple simultaneous clients with gsoap

If you look at my answer to this question you can see a very basic C++ threaded gSoap server. What I think you may be missing is the need to copy the service class, in my code the line tc = c.copy() ; // make a safe copy this copies the gSoap service instance including the gSoap context; it's this copy that's passed into the new thread so that the new thread can respond to the request while the main thread waits for another request to be made.

Is it possible to execute function in separate thread in gsoap?

1) I call this function in thread in my application.

2) Another person call this function in separate thread in their application.

If by application you mean the webservice client application the short answer is no that in itself won't cause gsoap to handle requests on multiple threads. gsoap doesn't provide any multithreading functionality out of the box. Launching a separate thread in the client application doesn't launch a corresponding thread on the (gsoap) server.

Your hunch though that multiple threads could be used to handle requests from multiple clients is right. However you'll need to create and manage those threads yourself in the gsoap application. You may find the this discussion helpful - gSOAP Multithreading



Related Topics



Leave a reply



Submit