#9654 closed Bugs (duplicate)
try_join_for(0) returns false when thread has exited
| Reported by: | Owned by: | Anthony Williams | |
|---|---|---|---|
| Milestone: | Component: | thread | |
| Version: | Boost 1.55.0 | Severity: | Regression |
| Keywords: | Cc: |
Description
I use try_join_for(boost::chrono::milliseconds(0)) to check if my thread has finished.
When upgrading from boost 53 to boost 55 I noticed that it now returns false systematically. It seems to work fine if I use any duration other than 0.
Here is a minimal code that reproduces the problem, I work with visual studio 2012 x64:
#include <boost\thread.hpp>
#include <iostream>
void my_func(void){
std::cout<<"This shouldn't take long...\n";
}
void main(void) {
boost::thread * t = new boost::thread(my_func);
int i = 0;
while (! t->try_join_for(boost::chrono::milliseconds(1))) {
std::cout<<"iteration "<<++i<<'\n';
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
std::cout << "first task finished\n";
delete t;
t = new boost::thread(my_func);
i=0;
while (! t->try_join_for(boost::chrono::milliseconds(0))) {
std::cout<<"iteration "<<++i<<'\n';
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
std::cout << "second task finished\n";
}
Attachments (1)
Change History (6)
follow-up: 2 comment:1 by , 9 years ago
comment:2 by , 9 years ago
Replying to ernest.galbrun@…:
Apparently it is caused by a previous bug fix: https://svn.boost.org/trac/boost/ticket/8323
Now try_join_for returns false immediately if the current time is >= to the finish time.
Right.
It should, I think, check at least once if the thread has completed before returning false.
Well, this is not what the documentation says :(
I'm not sure how to do that, that's the point of calling try_join_for(0).
Have you tried try_join_for(nanoseconds(1))?
Is there a reason why there is no try_join() function ?
I don't know of a portable way to implement it.
Please see #9618 and if you are on Windows, please could you check if the proposed patch works for you.
follow-up: 4 comment:3 by , 9 years ago
Hi viboes, thanks for your reply I had not seen the previous issue.
The patch works fine, thank you.
I think the documentation should be more explicit wether this is allowed or not, since this is the prefered way of checking if a thread has finished when searching on stackoverflow: http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method
Maybe we should change the SO answer, what is your stance about this issue ? What is the correct way of checking wether a thread has finished?
comment:4 by , 9 years ago
| Milestone: | To Be Determined |
|---|---|
| Resolution: | → duplicate |
| Status: | new → closed |
Replying to ernest.galbrun@…:
Hi viboes, thanks for your reply I had not seen the previous issue.
The patch works fine, thank you.
I think the documentation should be more explicit wether this is allowed or not, since this is the prefered way of checking if a thread has finished when searching on stackoverflow: http://stackoverflow.com/questions/1667420/how-can-i-tell-reliably-if-a-boost-thread-has-exited-its-run-method
Maybe we should change the SO answer, what is your stance about this issue ? What is the correct way of checking whether a thread has finished?
The answer of MSalters is the correct one.
You fundamentally can't do this. The reason is that the two possible answers are "Yes" and "Not when I last looked but perhaps now". There is no reliable way to determine that a thread is still inside its run method, even if there was a reliable way to determine the opposite.
You can just join the thread if not already detached.
by , 8 years ago
| Attachment: | 0001-changed-the-copy-from-rvalue-reference-to-copy-m_taa.patch added |
|---|
patch proposing fix

Apparently it is caused by a previous bug fix: https://svn.boost.org/trac/boost/ticket/8323
Now try_join_for returns false immediatly if the current time is >= to the finish time.
It should, I think, check at least once if the thread has completed before returning false. I'm not sure how to do that, that's the point of calling try_join_for(0).
Is there a reason why there is no try_join() function ?