Hello.
Is it feasible to make CVs' destructors thread-safe? Consider the code below. It causes a failed assertion in basic_condition_variable::dispose_entry(...) when cv's destructor kicks in while notify_all() is still cleaning up.
The problem is easily resolved by adding another mutex, but it would just look nicer if we could rely on cv's own features for that.
Another alternative would be to make wait() return only after notify_xxx() has really finished its job.
(Yes, I guess this code is not exactly a Good Practice :) , but it's not exactly logically inconsistent either. Or is it?)
[
Boost 1.35.0, MSVC2008, Win32 Debug configuration
Actually, it's not easy to make this particular code fail; adding a short sleep() in the beginning of dispose_entry(...) helps. This problem managed to appear quite systematically in a more complex program though.
]
----
Vladimir
:::CODE:::
#include <boost/thread.hpp>
#include <boost/bind.hpp>
class C {
private:
boost::mutex m;
boost::condition_variable cv;
public:
~C() {
boost::mutex::scoped_lock l( m );
cv.wait( l );
}
void fn() {
boost::this_thread::sleep( boost::posix_time::milliseconds( 500 ) );
cv.notify_all();
}
};
int main() {
C c;
boost::thread t( boost::bind( &C::fn, &c ) );
return 0;
}