boostorg / boostorg/thread

Destruction of boost::thread functor is later than with std::thread

Open
#380 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
214
Forks
171
PR merge metrics
No merged PRs in 30d

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:
```cpp
#include

#include
#include
#include

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
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");
test("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
```

[Wandbox](https://wandbox.org/permlink/kmRO1o8Yq9DV9SCd)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.