[thread] boost::thread automatically detached when thread terminates?
Is a boost::thread object automatically detached when a thread terminates? I read in the Boost.Thread 1.44 documentation: "If the thread of execution represented by the boost::thread object has already completed, or the boost::thread object represents Not-a-Thread, then join() returns immediately." Is it really required to call join() or timed_join()? Or can I also call joinable() which would return false then? Boris
On Sat, Sep 11, 2010 at 11:00:56PM +0200, Boris Schaeling wrote:
Is a boost::thread object automatically detached when a thread terminates?
I read in the Boost.Thread 1.44 documentation: "If the thread of execution represented by the boost::thread object has already completed, or the boost::thread object represents Not-a-Thread, then join() returns immediately."
Is it really required to call join() or timed_join()? Or can I also call joinable() which would return false then?
The four ways of cleaning up properly after a thread with Boost.Thread are: 1) detach() to make the boost::thread object not refer to a thread of execution anymore and any associated thread resources properly released; 2) have the boost::thread object die a natural death (upon which it will detach() in the destructor, resulting in 1); 3) join(), after return of the boost::thread will not refer to a thread of execution and the thread will have been cleaned up; 4) a successful timed_join(), with the same results as 3). Note that in no case it's sane to do things like { new thread(&f); }, as you're not only leaking a thread object, but also OS thread primitives, resulting in zombies and resource exhaustion. I hope this clears some things up. -- Lars Viklund | zao@acc.umu.se
On Sat, 11 Sep 2010 23:22:12 +0200, Lars Viklund
On Sat, Sep 11, 2010 at 11:00:56PM +0200, Boris Schaeling wrote:
Is a boost::thread object automatically detached when a thread terminates?
I read in the Boost.Thread 1.44 documentation: "If the thread of execution represented by the boost::thread object has already completed, or the boost::thread object represents Not-a-Thread, then join() returns immediately."
Is it really required to call join() or timed_join()? Or can I also call joinable() which would return false then?
The four ways of cleaning up properly after a thread with Boost.Thread are:
Sorry, I wasn't clear then. I'm trying to find out how I can easily tell whether a thread of execution has terminated: boost::thread t(...); // thread of execution runs bool running = t.joinable(); // thread of execution terminates (automatically) bool still_running = t.joinable(); Does joinable() return true in the first case and false in the second? The documentation says that in the second case join() returns immediately. However I don't want to call join() because it would block in the first case. I would need to know in advance whether the thread of execution has terminated or not - this is exactly what I'd like boost::thread to tell me. :) Boris
Hi Boris,
On Sun, Sep 12, 2010 at 12:04 AM, Boris Schaeling
Sorry, I wasn't clear then. I'm trying to find out how I can easily tell whether a thread of execution has terminated:
boost::thread t(...); // thread of execution runs bool running = t.joinable(); // thread of execution terminates (automatically) bool still_running = t.joinable();
Does joinable() return true in the first case and false in the second?
No, it will return true both times.
The documentation says that in the second case join() returns immediately. However I don't want to call join() because it would block in the first case. I would need to know in advance whether the thread of execution has terminated or not - this is exactly what I'd like boost::thread to tell me. :)
Maybe you can use boost::thread::timed_join() with a very low timeout (or even zero timeout, haven't checked if it works like that).
Boris
Cheers, Francesco.
On Sun, 12 Sep 2010 06:52:49 +0200, Francesco Biscani
[...]
boost::thread t(...); // thread of execution runs bool running = t.joinable(); // thread of execution terminates (automatically) bool still_running = t.joinable();
Does joinable() return true in the first case and false in the second?
No, it will return true both times.
The documentation says that in the second case join() returns immediately. However I don't want to call join() because it would block in the first case. I would need to know in advance whether the thread of execution has terminated or not - this is exactly what I'd like boost::thread to tell me. :)
Maybe you can use boost::thread::timed_join() with a very low timeout (or even zero timeout, haven't checked if it works like that).
Thanks for your reply! Do you know whether the thread of execution is allowed to call boost::thread::detach()? Then it could detach itself from the boost::thread object before it terminates and boost::thread::joinable() should work? I'm not sure though if boost::thread is thread-safe? Boris
On 9/12/2010 7:39 AM, Boris Schaeling wrote:
Thanks for your reply! Do you know whether the thread of execution is allowed to call boost::thread::detach()? Then it could detach itself from the boost::thread object before it terminates and boost::thread::joinable() should work? I'm not sure though if boost::thread is thread-safe?
What are you trying to accomplish? If you don't care when a thread terminates, call detach right after you create it: main thread: boost::thread t (...); t.detach ();
On Sun, 12 Sep 2010 14:44:42 +0200, Eric J. Holtman
On 9/12/2010 7:39 AM, Boris Schaeling wrote:
Thanks for your reply! Do you know whether the thread of execution is allowed to call boost::thread::detach()? Then it could detach itself from the boost::thread object before it terminates and boost::thread::joinable() should work? I'm not sure though if boost::thread is thread-safe?
What are you trying to accomplish?
I want a worker thread to terminate if there is nothing to do. If there is something to do again and the worker thread has terminated I need to create a new one. Now I wonder if I can use boost::thread to detect whether the thread is running or not. Boris
Thanks for your reply! Do you know whether the thread of execution is allowed to call boost::thread::detach()? Then it could detach itself from the boost::thread object before it terminates and boost::thread::joinable() should work? I'm not sure though if boost::thread is thread-safe?
I want a worker thread to terminate if there is nothing to do. If there is something to do again and the worker thread has terminated I need to create a new one. Now I wonder if I can use boost::thread to detect whether the thread is running or not.
How about using boost::asio. It automatically deals with worker threads and such. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
On Sun, 12 Sep 2010 15:12:35 +0200, Ray Burkholder
Thanks for your reply! Do you know whether the thread of execution is allowed to call boost::thread::detach()? Then it could detach itself from the boost::thread object before it terminates and boost::thread::joinable() should work? I'm not sure though if boost::thread is thread-safe?
I want a worker thread to terminate if there is nothing to do. If there is something to do again and the worker thread has terminated I need to create a new one. Now I wonder if I can use boost::thread to detect whether the thread is running or not.
How about using boost::asio. It automatically deals with worker threads and such.
I need the worker thread within a Boost.Asio extension. ;) Boris
How about using boost::asio. It automatically deals with worker
threads
and such.
I need the worker thread within a Boost.Asio extension. ;)
You can create multiple asio services, with each service having its own worker thread(s). So the extension, would have its own service, which deals with the worker thread(s) for you. Possibly over kill, but is the easy way out from reinventing the wheel. Or I may just not understand the problem. Ray -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
On 9/12/2010 7:55 AM, Boris Schaeling wrote:
I want a worker thread to terminate if there is nothing to do. If there is something to do again and the worker thread has terminated I need to create a new one. Now I wonder if I can use boost::thread to detect whether the thread is running or not.
The reason boost::thread doesn't provide you with an "is_running" function, is that it would be nearly impossible to write it correctly. Example: main thread starts worker. worker starts work. main thread calls is_running which returns true. now, before main thread can execute next statement, worker thread exits. main thread tries to send more work to worker, who is gone.
On Sun, 12 Sep 2010 15:32:13 +0200, Eric J. Holtman
On 9/12/2010 7:55 AM, Boris Schaeling wrote:
I want a worker thread to terminate if there is nothing to do. If there is something to do again and the worker thread has terminated I need to create a new one. Now I wonder if I can use boost::thread to detect whether the thread is running or not.
The reason boost::thread doesn't provide you with an "is_running" function, is that it would be nearly impossible to write it correctly.
Example:
main thread starts worker. worker starts work. main thread calls is_running which returns true.
now, before main thread can execute next statement, worker thread exits.
main thread tries to send more work to worker, who is gone.
Good point (and so obvious :)! Thanks, Boris
On 09/12/2010 02:55 PM, Boris Schaeling wrote:
I want a worker thread to terminate if there is nothing to do. If there is something to do again and the worker thread has terminated I need to create a new one. Now I wonder if I can use boost::thread to detect whether the thread is running or not.
Boris
You might also want to think about a thread pool: A number of worker threads which do something like this: while(true) { wait_for_task; process_task; } Rather simple to implement and more effective (I think) than starting a new thread for each task. It also allows you to control the number of total worker threads easily. It is also simple to keep track of the number of working/waiting worker threads. Regards, Roland
participants (6)
-
Boris Schaeling
-
Eric J. Holtman
-
Francesco Biscani
-
Lars Viklund
-
Ray Burkholder
-
Roland Bock