Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco::Thread FreeOnTerminate

Please post support and help requests here.

Poco::Thread FreeOnTerminate

Postby joon » 12 May 2008, 20:37

Hi all,

In Borland you get an option in threads called FreeOnTerminate. When this is set the thread will delete itself on completion. This functionality is great for fire-and-forget threads.
So far I have been unable to find similar functionality in the Poco, can anyone please point me in the right direction, even if there is no direction :)

Thx

Joon
joon
 
Posts: 3
Joined: 12 May 2008, 16:07

Re: Poco::Thread FreeOnTerminate

Postby guenter » 13 May 2008, 07:26

What you can do (and what is usually done) in POCO is to create a new Thread object on the heap, and then do a
Code: Select all

delete this;

at the end of the run() member function.

Alternatively, take a look at ThreadPool, which might be more appropriate for your case.

guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Poco::Thread FreeOnTerminate

Postby joon » 13 May 2008, 11:14

Hi Guenter,

My thread programming is a bid rusty, but is a good idea to delete the thread you are currently running in? Does the execution of the thread stop dead if it is deleted?

My class is derived from Poco::Runnable and has the thread as a private member (non-pointer).

The ThreadPool is a total overkill for what I need to do. Only one thread for this processing will be running at any given time.

I must compliment the Poco project, apart from this one issue the migration from Borland/Indy to Poco for server development was easy and simple. The library is a joy to work with.(:biggrin:)

Thx Joon
joon
 
Posts: 3
Joined: 12 May 2008, 16:07

Re: Re: Poco::Thread FreeOnTerminate

Postby joon » 13 May 2008, 11:45

> What you can do (and what is usually done) in POCO is to create a new Thread object on the heap, and then do a
>
Code: Select all

> delete this;
>

> at the end of the run() member function.
>
> Alternatively, take a look at ThreadPool, which might be more appropriate for your case.
>
>

Calling “delete this;” in the run() function blocks in
Code: Select all
Poco::ThreadImpl::joinImpl
and the pointer is not released.
joon
 
Posts: 3
Joined: 12 May 2008, 16:07

Re: Poco::Thread FreeOnTerminate

Postby guenter » 13 May 2008, 14:34

Normallly it should not be a problem to delete the Thread object while the thread is still running. The Thread class has been designed for this and in this case will detach the thread in the destructor. This means, however, that you can't join() the thread from another thread once it has been deleted. Also, Thread::current() will return an invalid pointer once the Thread object has been deleted.

Also, my first response was, uh, crap. While deleting the Thread technically should work, it's not really what I meant to say. The best way to solve this problem is to use the ThreadPool (I wouldn't consider it overkill, since it's there anyway - other parts of POCO use it, and it's really lightweight), and create a Runnable on the heap. Then, at the end of run, do the delete this - deleting the Runnable, not the thread. Now, you could also have the Thread as a member of your Runnable, In this case, the Thread object will be deleted with the Runnable and the underlying OS thread will be detached and run until your Runnable::run() completes. As I said above, join() won't work in this case. If you need some synchronization (tell someone that the thread has finished), use an Event object (stored outside of your Runnable) and set() it at the end of run(). You can wait() on this Event from another thread.

If you use the trunk version from POCO it is also possible to reuse a Thread object. You create the Thread object once and you can call start() multiple times, provided the thread has completed in between calls to start().
guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Re: Poco::Thread FreeOnTerminate

Postby alex » 13 May 2008, 15:26

> If you use the trunk version from POCO it is also possible to reuse a Thread object.

This has also been backported to 1.3.3 branch by Serge.

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

Re: Poco::Thread FreeOnTerminate

Postby ryppka » 13 May 2008, 22:35

Hi,

> In Borland you get an option in threads called FreeOnTerminate. When this is set the thread will delete itself on completion. This functionality is great for fire-and-forget threads.

> So far I have been unable to find similar functionality in the Poco, can anyone please point me in the right direction, even if there is no direction :)

If you need just one Thread/Task of "fire-n-forget" kind, if'd suggest to consider ActiveMethod/Activity.
ryppka
 
Posts: 4
Joined: 16 Feb 2008, 18:51
Location: Other


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest