Overview
Features
Download
Documentation
Community
Add-Ons & Services

Let main thread wait

A general discussion forum.

Let main thread wait

Postby fbraem » 22 Jun 2010, 19:54

I try to add TCPServer and family into GLUEscript. I have the problem that when the script ends (which runs in the main thread), all child threads ends too. I'm not familiar with threading in C/C++, so I'm looking for the best way to let the main thread wait for all its child threads to end.
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 102
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: Let main thread wait

Postby guenter » 22 Jun 2010, 21:17

To wait for completion of a specific Poco::Thread, use join(). The TCPServer uses the default Poco::ThreadPool unless set-up otherwise, so you can use joinAll(), as in:

Code: Select all
Poco::ThreadPool::defaultPool().joinAll();


to wait for the completion of all threads started by TCPServer.
guenter
 
Posts: 1110
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Let main thread wait

Postby fbraem » 22 Jun 2010, 23:15

guenter wrote:
Code: Select all
Poco::ThreadPool::defaultPool().joinAll();


This doesn't work. My main program is still exiting, even when TCPServer is started.
I've tried to use my own pool, but that gives the same result.
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 102
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: Let main thread wait

Postby guenter » 22 Jun 2010, 23:32

Sorry, I've missed an important detail...

You'll probably need a Poco::Event that you wait() for in you main thread (before joining the threads from the pool). When it's time to stop the TCPServer, set() the event object from another thread and the call to wait() will return. Many POCO-based server applications call Poco::Util::Application::waitForTerminationRequest() after setting up the TCPServer (or HTTPServer, etc.). waitForTerminationRequest() will block until the user enters CTRL-C or kills the process. You want something similar for your scripts.
guenter
 
Posts: 1110
Joined: 11 Jul 2006, 16:27
Location: Austria


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 2 guests