From 75ed067b7ddaf0a2081ba6e083fd37fb3f14ced2 Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Fri, 19 Apr 2002 18:03:48 +0000 Subject: [PATCH] allow derived class destructor to wait for thread exit --- src/libCom/osi/epicsThread.cpp | 31 +++++++++++++++++++++++++------ src/libCom/osi/epicsThread.h | 3 +++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/libCom/osi/epicsThread.cpp b/src/libCom/osi/epicsThread.cpp index 8cc845303..853c47e37 100644 --- a/src/libCom/osi/epicsThread.cpp +++ b/src/libCom/osi/epicsThread.cpp @@ -6,8 +6,7 @@ #include #include - - +#include #define epicsExportSharedSymbols #include "epicsThread.h" @@ -24,11 +23,25 @@ void epicsThreadCallEntryPoint ( void *pPvt ) pThread->runable.run (); } pThread->id = 0; + pThread->terminated = true; pThread->exit.signal (); } +void epicsThread::exitWait () +{ + assert ( this->exitWait ( DBL_MAX ) ); +} + +bool epicsThread::exitWait ( double delay ) +{ + if ( ! this->terminated ) { + this->exit.wait ( delay ); + } + return this->terminated; +} + epicsThread::epicsThread (epicsThreadRunable &r, const char *name, unsigned stackSize, unsigned priority ) : - runable(r), cancel (false) + runable(r), cancel (false), terminated ( false ) { this->id = epicsThreadCreate ( name, priority, stackSize, epicsThreadCallEntryPoint, static_cast (this) ); @@ -39,9 +52,15 @@ epicsThread::~epicsThread () if ( this->id ) { this->cancel = true; this->begin.signal (); - while ( ! this->exit.wait ( 5.0 ) ) { - printf ("epicsThread::~epicsThread ():" - " Warning, thread object destroyed before thread exit \n"); + while ( ! this->exitWait ( 10.0 ) ) { + char nameBuf [256]; + this->getName ( nameBuf, sizeof ( nameBuf ) ); + fprintf ( stderr, + "epicsThread::~epicsThread(): " + "blocking for thread \"%s\" to exit", + nameBuf ); + fprintf ( stderr, + "was epicsThread object destroyed before thread exit ?\n"); } } } diff --git a/src/libCom/osi/epicsThread.h b/src/libCom/osi/epicsThread.h index 60d3d1448..9c8360762 100644 --- a/src/libCom/osi/epicsThread.h +++ b/src/libCom/osi/epicsThread.h @@ -119,6 +119,8 @@ public: unsigned int priority=epicsThreadPriorityLow); virtual ~epicsThread (); void start(); + void exitWait (); + bool exitWait ( double delay ); void resume (); void getName (char *name, size_t size) const; unsigned int getPriority () const; @@ -139,6 +141,7 @@ private: epicsEvent exit; epicsEvent begin; bool cancel; + bool terminated; epicsThread ( const epicsThread & ); epicsThread & operator = ( const epicsThread & );