allow derived class destructor to wait for thread exit

This commit is contained in:
Jeff Hill
2002-04-19 18:03:48 +00:00
parent 3a6422e7af
commit 75ed067b7d
2 changed files with 28 additions and 6 deletions
+25 -6
View File
@@ -6,8 +6,7 @@
#include <stdio.h>
#include <stddef.h>
#include <float.h>
#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 <void *> (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");
}
}
}
+3
View File
@@ -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 & );