From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,3b3321b91f88c41e,start X-Google-Attributes: gid109fba,public X-Google-Thread: f678f,3b3321b91f88c41e,start X-Google-Attributes: gidf678f,public X-Google-ArrivalTime: 1994-05-18 12:34:12 PST Newsgroups: comp.lang.c++,comp.unix.osf.misc Path: bga.com!news.sprintlink.net!hookup!swrinde!news.uh.edu!uuneo.neosoft.com!jabberwock!daniels From: daniels@biles.com (Brad Daniels) Subject: Race condition during object destruction. How to avoid? Organization: Biles and Associates Date: Wed, 18 May 1994 18:18:58 GMT Message-ID: Xref: bga.com comp.lang.c++:15850 comp.unix.osf.misc:539 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) + -----------------------------------------------------------------------------