Overview
Features
Download
Documentation
Community
Add-Ons & Services

terminating http server

Please post support and help requests here.

terminating http server

Postby ruediger » 16 Jun 2007, 00:21

Hi there,

POCOs HTTPTimeServer example has a sequence in the HTTPTimeServer::main() function that says:

HTTPServer srv(new TimeRequestHandlerFactory(), svs, pParams);

srv.start();

waitForTerminationRequest();

srv.stop();

As I understand it, waitForTerminationRequest() waits for Ctrl-C or a kill signal. This would mean that the srv.stop() part of the function is never reached.

I'm looking for a way to wait for the server thread (started by srv.start() ) to finish and then continues with the execution, so I can cleanly shut down other stuff.

Do you have a suggestion ?

terminate() seems to just send a kill signal, and code beyond waitForTerminationRequest is never reached.

I have also tried to set a mutex in the constructor, then try to lock the mutex right before srv.stop() , which of course blocks. The idea was to release the lock as last action of the server thread. However, for reasons I do not understand, this does not work.

In any case thanks for your help!

Best Regards (and a good night),
Ruediger Berlich
ruediger
 
Posts: 3
Joined: 09 Jun 2007, 17:42

Re: terminating http server

Postby alex » 16 Jun 2007, 14:05

> As I understand it, waitForTerminationRequest() waits for Ctrl-C or a kill signal. This would mean that the srv.stop() part of the function is never reached.

It is reached. That's what waitForTerminationRequest() does - intercepts the termination so application can exit cleanly.

If you modify the code in HTTPtimeServer like this:

Code: Select all

//...
std::cout << "before waitForTerminationRequest()" << std::endl;
// wait for CTRL-C or kill
waitForTerminationRequest();
std::cout << "after waitForTerminationRequest()" << std::endl;
std::cout << "before srv.stop()" << std::endl;
// Stop the HTTPServer
srv.stop();
std::cout << "after srv.stop()" << std::endl;
//...


and run it as console app, you get this in console:

Code: Select all

before waitForTerminationRequest()
after waitForTerminationRequest()
before srv.stop()
after srv.stop()
Press any key to continue . . .



> I'm looking for a way to wait for the server thread (started by srv.start() ) to finish and then continues with the execution, so I can cleanly shut down other stuff.
>
> Do you have a suggestion ?

Since srv.stop() is reached, there lies the answer.

> terminate() seems to just send a kill signal, and code beyond waitForTerminationRequest is never reached.

I wanted to double-check this, so I ran it in debugger (MS Visual Studio 8) and it does reach srv.stop(). If the debugger complains about ctrl-c (can be disabled in IDE settings), just choose "continue" and application will exit cleanly.

> I have also tried to set a mutex in the constructor, then try to lock the mutex right before srv.stop() , which of course blocks. The idea was to release the lock as last action of the server thread. However, for reasons I do not understand, this does not work.

There may be some other reason for your troubles (hard to tell without seeing all the code), but it is not the one stated in this post.

HTH

Alex
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron