Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#9654 closed Bugs (duplicate)

try_join_for(0) returns false when thread has exited

Reported by: ernest.galbrun@… 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)

0001-changed-the-copy-from-rvalue-reference-to-copy-m_taa.patch (2.0 KB ) - added by ernest.galbrun@… 8 years ago.
patch proposing fix

Download all attachments as: .zip

Change History (6)

comment:1 by ernest.galbrun@…, 9 years ago

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 ?

in reply to:  1 comment:2 by viboes, 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.

comment:3 by ernest.galbrun@…, 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?

in reply to:  3 comment:4 by viboes, 9 years ago

Milestone: To Be Determined
Resolution: duplicate
Status: newclosed

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.

comment:5 by viboes, 9 years ago

Duplicate #9618

by ernest.galbrun@…, 8 years ago

patch proposing fix

Note: See TracTickets for help on using tickets.