Usenet comp.unix.osf.misc (1992-2004)
 help / color / mirror / Atom feed
* Race condition during object destruction.  How to avoid?
@ 1994-05-18 18:18 Brad Daniels
  1994-05-19 15:29 ` Christopher Barber
  0 siblings, 1 reply; 3+ messages in thread
From: Brad Daniels @ 1994-05-18 18:18 UTC (permalink / 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)   +
-----------------------------------------------------------------------------


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Race condition during object destruction.  How to avoid?
  1994-05-18 18:18 Race condition during object destruction. How to avoid? Brad Daniels
@ 1994-05-19 15:29 ` Christopher Barber
  1994-05-19 20:48   ` Brad Daniels
  0 siblings, 1 reply; 3+ messages in thread
From: Christopher Barber @ 1994-05-19 15:29 UTC (permalink / raw)


>>>>> "BD" == Brad Daniels <daniels@biles.com> writes:

    BD> I have a class which models pthread condition variables.  I'd like
    BD> to make a subclass of that class which will wake up at fixed
    BD> intervals regardless of other conditions.  To achieve this, I tried
    BD> making a class with an associated thread which periodically signals
    BD> the condition variable to wake up (after forcing the predicate to
    BD> be true)....
    
Why not save yourself a lot of trouble and implement the wait() call on the
subclass using pthread_cond_timedwait()?

- Chris
--
Christopher Barber
(cbarber@bbn.com)


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Race condition during object destruction.  How to avoid?
  1994-05-19 15:29 ` Christopher Barber
@ 1994-05-19 20:48   ` Brad Daniels
  0 siblings, 0 replies; 3+ messages in thread
From: Brad Daniels @ 1994-05-19 20:48 UTC (permalink / raw)


In article <CBARBER.94May19112914@apricot.bbn.com>,
Christopher Barber <cbarber@bbn.com> wrote:
>>>>>> "BD" == Brad Daniels <daniels@biles.com> writes:
>
>    BD> I have a class which models pthread condition variables.  I'd like
>    BD> to make a subclass of that class which will wake up at fixed
>    BD> intervals regardless of other conditions.  To achieve this, I tried
>    BD> making a class with an associated thread which periodically signals
>    BD> the condition variable to wake up (after forcing the predicate to
>    BD> be true)....
>    
>Why not save yourself a lot of trouble and implement the wait() call on the
>subclass using pthread_cond_timedwait()?

That is actually the work-around I put in, though I have to use
pthread_getexpiration_np in order to figure out the end time, and I hate
using _np routines.  Another part of the problem with this approach is
that the timing is less reliable.  I have to determine the time for the next
read before commencing the read, so that my interval isn't (time for read to
complete) + interval instead of just the given interval.  Also, if a network
event causes a read to take longer than the interval, I will get two reads,
one immediately after the other, with the next read thereafter commencing
at a new offset (e.g. I might get reads starting at 10 sec, 20 sec, 30 sec,
42 sec, 52 sec, etc.)  For my current purposes, this shouldn't be a problem,
but I'd like to code something more reliable if possible.

- Brad
------------------------------------------------------------
+ Brad Daniels                  | Was mich nicht umbringt  +
+ Biles and Associates          | macht mich hungrig.      +
+ These are my views, not B&A's |                          +
------------------------------------------------------------


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1994-05-19 20:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-05-18 18:18 Race condition during object destruction. How to avoid? Brad Daniels
1994-05-19 15:29 ` Christopher Barber
1994-05-19 20:48   ` Brad Daniels

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