Boost logo

Boost :

From: Roland (roland.schwarz_at_[hidden])
Date: 2003-11-12 07:47:40


Hi,

I am not sure wether this question has a simple answer, but perhaps someone can give me some hints.

I am trying to use the monitor pattern with boost::thread and came to the question of what steps are sufficient to correctly destroy my monitored object.

In the constructor I am tempted to:

myobj::~myobj {
   lock lk(monitor);
   canceled = true;
   cond1.notify_all();
   cond2.notify_all();
   // .. all conditions
}

If I handle the canceled variable in my waiting functions I can return or throw out of the waiting functions from there. BUT this will happen AFTER my destructor has released monitor, and therefore destroyed the object. So what will happen to my locks, which are members of the object, or my canceled variable?

So if I:

myobj::~myobj {
     {
      lock lk(monitor);
      canceled = true;
      }
   cond1.notify_all();
   cond2.notify_all();
   // .. all conditions
}

This will give my waiting functions a chance to execute, BUT the destructor wont wait until they are finished. So it seems to me as if I would need
a second condition to acknowledge that all waiting functions have ended. Hmm, and a variable that keeps track how many waiters there have been, as I have to know when the last one has been released.

Did I forget anything?

This sound very complex to me. Is there a simpler solution?

Thank you in advance,
Roland


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk