Home › Forums › C Programming › multithreading on OSX
- This topic has 1 reply, 1 voice, and was last updated 16 years, 6 months ago by si_icarus.
- AuthorPosts
- June 21, 2008 at 6:55 am #2109si_icarusParticipant
Hi all, I have a query about multithreading. What I would like to do is, at the start of my main update() function, start a couple of threads in parallel, once they are all complete carry on with my main update function.
123456789101112131415161718<br />void update() {<br />thread1->update(); // fluid solver<br />thread2->update(); // particle system<br />thread3->update(); // camera1 optical flow<br />thread4->update(); // camera2 optical flow<br />// all of the above threads have their own data and work independantly<br /><br />etc.<br />.<br />.. wait till all the above threads are done.<br />.. once they are done, feed the data from each of them into other ones<br />.. e.g. fluid velocity info is added to particle systems reedy for next frame's update<br />.. e.g. camera optical flow data is fed into fluid solver, ready for next frame's update<br /><br />render all<br />}<br />I am having trouble getting this working the way I want. I don’t want my threads to loop continuously independent of my main update loop, I want them all to start at the beginning of my update loop, and then once they are all finished, feed the data around, then render, then repeat.
I tried creating a thread every update loop which runs only once (no while() in the function) – it works and behaves fine for a few minutes, then the threads just stop being created – no crash or hang, just no updates from threads. I also tried using an infinte while() but with a flag that determined whether the code ran that frame or not, and only set that flag at the beginning of the main update loop (code for this below) – that one is very stable, but behaviour was weird, I think the thread updates weren’t being called when I wanted them to.
I’ve read a lot of documentation on multithreading in general and all the pthread commands. I understand the fundamentals of protecting data and all that.. but I’m just lacking a lot of experience and strategy .. so any suggestions welcome!
This is my thread class I’m using which has the infinite loop in the threaded function, but runs the update function if the flag is set. I initialize the flag at the beginning of each main update with thread1->runOnce(); thread2->runOnce(); etc…. but it doesn’t run smoothly.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253<br /><br />#include "ofxThread.h"<br /><br />class MSAThread : public ofxThread {<br /><br />bool bHasRunThisFrame;<br />bool bAutoLoop;<br />int interval;<br /><br />public:<br /><br />// create the thread, if autoLoop is true, start running straight away and loop,<br />// otherwise wait for the runOnce command<br />void start(int frameRate = 30, bool initAutoLoop = false){<br />interval = 1000/frameRate;<br />bAutoLoop = initAutoLoop;<br />if(!bAutoLoop) bHasRunThisFrame = true; // don't run straight away<br />startThread(true, false); // blocking, verbose<br />}<br /><br />// run the update function once in another thread<br />void runOnce() {<br />if(lock()) {<br />bAutoLoop = false;<br />bHasRunThisFrame = false;<br />unlock();<br />}<br />}<br /><br />// run the update function in another thread<br />void threadedFunction() {<br />while(isThreadRunning()) {<br />if( lock() ){<br />if(bAutoLoop || !bHasRunThisFrame) {<br />update();<br />bHasRunThisFrame = true;<br />unlock();<br />ofSleepMillis(interval);<br />} else {<br />unlock();<br />}<br />}<br />}<br />}<br /><br />// main thread waits for the update function to finish<br />void waitForFinish() { if(lock()) { unlock(); } }<br /><br />// this is the function that gets overvidden<br />virtual void update() = 0;<br />};<br />and it’s based on this OpenFrameworks class:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169<br />class ofxThread{<br /><br />public:<br />ofxThread();<br />virtual ~ofxThread();<br />bool isThreadRunning();<br />void startThread(bool _blocking = true, bool _verbose = true);<br />bool lock();<br />bool unlock();<br />void stopThread();<br /><br />protected:<br /><br />//<hr class="bbcode_rule" />//you need to overide this with the function you want to thread<br />virtual void threadedFunction(){<br />if(verbose)printf("ofxThread: overide threadedFunction with your ownn");<br />}<br /><br />//<hr class="bbcode_rule" /><br />#ifdef TARGET_WIN32<br />static unsigned int __stdcall thread(void * objPtr){<br />ofxThread* me = (ofxThread*)objPtr;<br />me->threadedFunction();<br />return 0;<br />}<br /><br />#else<br />static void * thread(void * objPtr){<br />ofxThread* me = (ofxThread*)objPtr;<br />me->threadedFunction();<br />return 0;<br />}<br />#endif<br /><br /><br />#ifdef TARGET_WIN32<br />HANDLE myThread;<br />CRITICAL_SECTION critSec; //same as a mutex<br />#else<br />pthread_t myThread;<br />pthread_mutex_t myMutex;<br />#endif<br /><br />bool threadRunning;<br />bool blocking;<br />bool verbose;<br />};<br /><br /><br />//<hr class="bbcode_rule" />ofxThread::ofxThread(){<br />threadRunning = false;<br />}<br /><br />//<hr class="bbcode_rule" />ofxThread::~ofxThread(){<br />stopThread();<br />}<br /><br />//<hr class="bbcode_rule" />bool ofxThread::isThreadRunning(){<br />//should be thread safe - no writing of vars here<br />return threadRunning;<br />}<br /><br />//<hr class="bbcode_rule" />void ofxThread::startThread(bool _blocking, bool _verbose){<br /><br />//have to put this here because the thread can be running<br />//before the call to create it returns<br />threadRunning = true;<br /><br />#ifdef TARGET_WIN32<br />InitializeCriticalSection(&critSec);<br />myThread = (HANDLE)_beginthreadex(NULL, 0, this->thread, (void *)this, 0, NULL);<br />#else<br />pthread_mutex_init(&myMutex, NULL);<br />pthread_create(&myThread, NULL, thread, (void *)this);<br />#endif<br /><br />blocking = _blocking;<br />verbose = _verbose;<br />}<br /><br />//<hr class="bbcode_rule" />//returns false if it can't lock<br />bool ofxThread::lock(){<br />if(!threadRunning){<br />if(verbose)printf("ofxThread: need to call startThread firstn");<br />return false;<br />}<br /><br />#ifdef TARGET_WIN32<br />if(blocking)EnterCriticalSection(&critSec);<br />else {<br />if(!TryEnterCriticalSection(&critSec)){<br />if(verbose)printf("ofxThread: mutext is busy n");<br />return false;<br />}<br />}<br />if(verbose)printf("ofxThread: we are in -- mutext is now locked n");<br />#else<br /><br />if(blocking){<br />if(verbose)printf("ofxThread: waiting till mutext is unlockedn");<br />pthread_mutex_lock(&myMutex);<br />if(verbose)printf("ofxThread: we are in -- mutext is now locked n");<br />}else{<br />int value = pthread_mutex_trylock(&myMutex);<br />if(value == 0){<br />if(verbose)printf("ofxThread: we are in -- mutext is now locked n");<br />}<br />else{<br />if(verbose)printf("ofxThread: mutext is busy - already lockedn");<br />return false;<br />}<br />}<br />#endif<br /><br />return true;<br />}<br /><br />//<hr class="bbcode_rule" />bool ofxThread::unlock(){<br /><br />if(!threadRunning){<br />if(verbose)printf("ofxThread: need to call startThread firstn");<br />return false;<br />}<br /><br />#ifdef TARGET_WIN32<br />LeaveCriticalSection(&critSec);<br />#else<br />pthread_mutex_unlock(&myMutex);<br />#endif<br /><br />if(verbose)printf("ofxThread: we are out -- mutext is now unlocked n");<br /><br />return true;<br />}<br /><br />//<hr class="bbcode_rule" />void ofxThread::stopThread(){<br />if(threadRunning){<br />#ifdef TARGET_WIN32<br />CloseHandle(myThread);<br />#else<br />pthread_mutex_destroy(&myMutex);<br />pthread_detach(myThread);<br />#endif<br />if(verbose)printf("ofxThread: thread stoppedn");<br />threadRunning = false;<br />}else{<br />if(verbose)printf("ofxThread: thread already stoppedn");<br />}<br />}<br /> - June 21, 2008 at 8:05 am #3404si_icarusParticipant
Actually, I think I’ve actually identified the problem. I think it was the sleep() function in my thread loop. I understand why I need that if the loop is continuously running and doing heavy stuff, but do I need it in my case?
When I comment out the sleep line (As below), the behaviour looks a lot smoother and correct, maybe because as soon as I set the bHasRunThisFrame flag in the thread instance to false, the update function is called almost immediately (which is what I want), whereas with the sleep() function, maybe there was a delay of upto interval before the update kicked in?…. or something :P Is it bad to not have a sleep function in there?
// run the update function in another thread
123456789101112131415void MSAThread::threadedFunction() {<br />while(isThreadRunning()) {<br />if( lock() ){<br />if(bAutoLoop || !bHasRunThisFrame) {<br />update();<br />bHasRunThisFrame = true;<br />unlock();<br />// ofSleepMillis(interval);<br />} else {<br />unlock();<br />}<br />}<br />}<br />}<br />
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.