Boost logo

Boost Users :

From: Vladimir Pozdyaev (vpozdyaev_at_[hidden])
Date: 2008-07-25 04:49:47


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;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net