Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
From: daniels@biles.com (Brad Daniels)
Subject: Race condition during object destruction.  How to avoid?
Date: Wed, 18 May 1994 18:18:58 GMT	[thread overview]
Message-ID: <Cq0G7n.Mqq@biles.com> (raw)

I have a class which models pthread condition variables.  I'd like to
make a subclass of that class which will wake up at fixed intervals
regardless of other conditions.  To achieve this, I tried making a class
with an associated thread which periodically signals the condition variable
to wake up (after forcing the predicate to be true).

The problem comes when I try to destruct the object.  I need to ensure that
the thread gets stopped *before* the dtor begins execution.  The reason for
this is that some member functions which are accessed when the associated
thread wakes up will be virtual.  If I put the thread cancel in my class's
dtor, there is a chance that one of the virtual functions will get
called after the dtor in some subclass of my class has been completed,
but before my class's dtor gets called, meaning that the object will
be partially destructed and its vtable may no longer be valid.


E.g:

class A {
//...
public:
  virtual void signal() { /*...*/ }
  virtual bool predicate() = 0;
};

class B {
public:
  B() { /* start thread which calls signal() and predicate() */ }
  virtual ~B() { /* cancel thread */ }
  virtual void signal() { /*...*/ }
};

class C {
public:
  ~C() { /* mess up data members severely */ }
  virtual bool predicate() { /* Use local data members */ }
};

During destruction, ~C() gets called first, while the thread is still active.
If the thread calls predicate() after ~C() has messed up some or all of the
data, I'm in deep trouble.

I realize I could get around this problem to some extent by making the dtor
protected and providing dynamic creation and destruction routines, but I
really dislike that route.  I prefer to make them first-class objects.
I've been thinking I might be able to do something with a helper class
which delegates all the member functions to an object of the real class
(and could cancel the thread before commencing object destruction),
but that can be a real headache from a maintenance standpoint.

Any suggestions would be appreciated.

- Brad
-----------------------------------------------------------------------------
+ Brad Daniels                  | "For some are UNIX because they were born +
+ Biles and Associates          |  that way, others were made that way un-  +
+ These are my views,           |  naturally, and others are UNIX because   +
+ not B&A's                     |  of the kingdom of heaven" (Matt 19:12)   +
-----------------------------------------------------------------------------


             reply	other threads:[~1994-05-18 18:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-05-18 18:18 Brad Daniels [this message]
1994-05-19 15:29 ` Race condition during object destruction. How to avoid? Christopher Barber
1994-05-19 20:48   ` Brad Daniels
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox