Skip to content

Commit

Permalink
ofThread: detach thread on stop if it's not being waited. Closes #2506
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoc committed Jun 9, 2014
1 parent 0c90c15 commit 1d85c9e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion libs/openFrameworks/utils/ofThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@


//-------------------------------------------------
ofThread::ofThread(): _threadRunning(false), _mutexBlocks(true){
ofThread::ofThread()
:_threadRunning(false)
,_mutexBlocks(true)
,threadBeingWaitedFor(false){
thread.setName("Thread " + ofToString(thread.id()));
}

Expand Down Expand Up @@ -106,6 +109,8 @@ void ofThread::stopThread(){
//-------------------------------------------------
void ofThread::waitForThread(bool callStopThread, long milliseconds){
if(thread.isRunning()){
threadBeingWaitedFor = true;

// tell thread to stop
if(callStopThread){
stopThread();
Expand Down Expand Up @@ -199,6 +204,12 @@ void ofThread::run(){
// should loop endlessly.
threadedFunction();

#if !defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
// FIXME: this won't be needed once we update POCO https://github.com/pocoproject/poco/issues/79

This comment has been minimized.

Copy link
@bilderbuchi

bilderbuchi Jun 9, 2014

Member

Do we really use/heed FIXMEs and TODOs in code? Wouldn't it better to file an issue about it?

if(!threadBeingWaitedFor){ //if threadedFunction() ended and the thread is not being waited for, detach it before exiting.
pthread_detach(pthread_self());
}
#endif
#ifdef TARGET_ANDROID
attachResult = ofGetJavaVMPtr()->DetachCurrentThread();
#endif
Expand Down
2 changes: 2 additions & 0 deletions libs/openFrameworks/utils/ofThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,6 @@ class ofThread: protected Poco::Runnable {
Poco::AtomicCounter _mutexBlocks;
///< \brief Should the mutex block?

bool threadBeingWaitedFor;

};

0 comments on commit 1d85c9e

Please sign in to comment.