Destruction of boost::thread functor is later than with std::thread
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- cpp
- Domain
- operating-systems
Research direction
Start with the supplied main() and test() example, or reproduce it from the linked Wandbox, comparing std::thread and boost::thread behavior. The issue names no repository files or tests; done means determining whether the destruction difference is intentional and documenting or correcting it with evidence from the thread implementation.
Written by the indexing model from the issue text.
Description
It seems that boost::thread destroys its functor only when join is called. std::thread destroys its functor immediately after the functor returns. Is there a reason for this discrepancy?
Using RAII patterns this can lead to surprising deadlocks...
Example code:
#include <boost/thread/thread.hpp>
#include <cstdio>
#include <thread>
#include <utility>
struct foo {
const char* name_ = nullptr;
foo(const char* name) : name_(name) { printf("%s foo()\n", name); }
foo(foo&& o) noexcept : name_(std::exchange(o.name_, nullptr)) {}
foo(foo& o) = delete;
~foo() {
if (name_) {
printf("%s ~foo()\n", name_);
}
}
};
template <typename Thread>
void test(const char* name) {
printf("%s starting\n", name);
auto thread = Thread([&, f = foo(name)] { printf("%s done\n", name); });
std::this_thread::sleep_for(std::chrono::milliseconds(100));
printf("%s joining\n", name);
thread.join();
printf("%s joined\n\n", name);
}
int main() {
test<std::thread>("std::thread");
test<boost::thread>("boost::thread");
}
Output:
std::thread starting
std::thread foo()
std::thread done
std::thread ~foo()
std::thread joining
std::thread joined
boost::thread starting
boost::thread foo()
boost::thread done
boost::thread joining
boost::thread ~foo()
boost::thread joined
- Dominant language
- C++
- Stars
- 213
- Forks
- 171
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/thread
-
Difficulty 1/5 Under an hour Newbie friendliness 65/100
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 58/100
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
AXERA-TECH/ax-llm#77 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
games-on-whales/wolf#509 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
bug-unconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 76/100