I'm having a thread function which takes a weak_ptr<> and I pass my shared_ptr<> in the thread function.
Legally weak_ptr<> should not increment the reference count of shared_ptr<>, however, unless I typecast with weak_ptr<> while passing the same to the thread function, it increments the reference count (unexpected)
This behaviour happens only with thread functions and not with normal function calls.
Here is the code for thread function
void thrdfn(weak_ptr<int> wp) {
cout<<wp.use_count()<<endl; // Prints 2
}
int main() {
shared_ptr<int> sp = make_shared<int>();
thread th { thrdfn, (sp)};
th.join();
return 0;
}
However, when I typecast while creating thread, it behaves properly
void thrdfn(weak_ptr<int> wp) {
cout<<wp.use_count()<<endl; // Prints 1
}
int main() {
thread th { thrdfn, weak_ptr<int>(sp)}; // typecast
}
When I call the function as a normal function call, it works fine without typecasting
void thrdfn(weak_ptr<int> wp) {
cout<<wp.use_count()<<endl; // Prints 1
}
int main() {
shared_ptr<int> sp = make_shared<int>();
thrdfn(sp);
return 0;
}
Behaviour is same with multiple compilers
When you write construct a std::thread (and I removed the superfluous parentheses):
thread th{thrdfn, sp};
What happens is:
The new thread of execution starts executing
std::invoke(decay_copy(std::forward<Function>(f)), decay_copy(std::forward<Args>(args))...);where decay_copy is defined as
template <class T> std::decay_t<T> decay_copy(T&& v) { return std::forward<T>(v); }
Which is to say, your shared_ptr is copied into the thread and you take a weak_ptr off of that copy. So there are two shared_ptrs: yours and the thread's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With