Skip to content

Commit

Permalink
added waitForThread method. detach and join are incompatible so
Browse files Browse the repository at this point in the history
have added a close parameter to stopThread that defaults to true
but is called as false from the thread to not called detach.
wait has also a stop parameter to set threadRunning to false and then
wait for the thread to finish or only wait in case the thread will
stop by itself. Closes openframeworks#204
  • Loading branch information
arturoc committed Sep 24, 2010
1 parent 82361d1 commit 4159853
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
52 changes: 39 additions & 13 deletions addons/ofxThread/src/ofxThread.cpp
Expand Up @@ -97,17 +97,43 @@ bool ofxThread::unlock(){
}

//-------------------------------------------------
void ofxThread::stopThread(){
if(threadRunning){
#ifdef TARGET_WIN32
CloseHandle(myThread);
#else
//pthread_mutex_destroy(&myMutex);
pthread_detach(myThread);
#endif
if(verbose)printf("ofxThread: thread stopped\n");
threadRunning = false;
}else{
if(verbose)printf("ofxThread: thread already stopped\n");
}
void ofxThread::stopThread(bool close){
if(threadRunning){
if(close){
#ifdef TARGET_WIN32
CloseHandle(myThread);
#else
//pthread_mutex_destroy(&myMutex);
pthread_detach(myThread);
#endif
}
if(verbose)printf("ofxThread: thread stopped\n");
threadRunning = false;
}else{
if(verbose)printf("ofxThread: thread already stopped\n");
}
}

//-------------------------------------------------
void ofxThread::waitForThread(bool stop){
if (threadRunning){
// Reset the thread state
if(stop){
threadRunning = false;
if(verbose)printf("ofxThread: stopping thread\n");
}
if(verbose)printf("ofxThread: waiting for thread to stop\n");
// Wait for the thread to finish
#ifdef TARGET_WIN32
WaitForSingleObject(myThread, INFINITE);
CloseHandle(myThread);
#else
if(pthread_self()==myThread) printf("ofxThread: error, waitForThread should only be called from outside the thread");
pthread_join(myThread, NULL);
#endif
if(verbose)printf("ofxThread: thread stopped\n");
myThread = NULL;
}else{
if(verbose)printf("ofxThread: thread already stopped\n");
}
}
9 changes: 5 additions & 4 deletions addons/ofxThread/src/ofxThread.h
Expand Up @@ -7,7 +7,6 @@
#include <process.h>
#else
#include <pthread.h>
#include <semaphore.h>
#endif

class ofxThread{
Expand All @@ -19,7 +18,8 @@ class ofxThread{
void startThread(bool _blocking = true, bool _verbose = true);
bool lock();
bool unlock();
void stopThread();
void stopThread(bool close = true);
void waitForThread(bool stop = true);

protected:

Expand All @@ -35,15 +35,16 @@ class ofxThread{
static unsigned int __stdcall thread(void * objPtr){
ofxThread* me = (ofxThread*)objPtr;
me->threadedFunction();
me->stopThread();
me->stopThread(false);
return 0;
}

#else
static void * thread(void * objPtr){
ofxThread* me = (ofxThread*)objPtr;
me->threadedFunction();
me->stopThread();
me->stopThread(false);
pthread_exit(NULL);
return 0;
}
#endif
Expand Down

0 comments on commit 4159853

Please sign in to comment.