Re: multithreading on OSX
Quote:
NO if you only do one print from each thread, then you will see the output s consequtibvely. Make it so that there are muiltiple outputs with a sleep or yielf and you will se the correct results.
I see what you mean, I've modified the thread update function to printf during the for and it is true that all the threads are working, but unfortunately the frame counts are differing and growing out of sync quite rapidly. THis is only an issue when the thread updates vary in length (i.e. when I do a constant long for loop they stay in sync, but when I do the for upto a random number, they grow out of sync rapidly).
Quote:
BIG mistake. It may be stable now, but what load is it putting on the system??? And ANY change to your environment may introduce destabilization.
I'd much rather do it the proper way with the condition variables, but I just can't get it to work! I've included my code below. Do I need both the sleep commands in the thread update and main update? And why are the framecounts growing out of sync?
Code:
void threadedFunction(){
while(threadRunning) {
lock();
pthread_cond_wait(&cvWakeup, &myMutex);
counter++;
update();
unlock();
ofSleepMillis(1);
}
}
void wakeUp() {
lock();
pthread_cond_signal(&cvWakeup);
unlock();
}
void waitForFinish() {
lock();
unlock();
}
virtual void update() {
unsigned int n = random();
float f = 0;
for(int i=0; i < n; i++) {
if(i%10000000 == 0) printf(" { %i WORKING } ", index);
f = cos(sin(i));
}
printf(" { %i:%i } ", index, counter);
}
Code:
void App::update(){
printf("\n[ **** %i : ", ofGetFrameNum());
for(int i=0; i<NUM_THREADS; i++) thread[i].wakeUp();
ofSleepMillis(1);
for(int i=0; i<NUM_THREADS; i++) thread[i].waitForFinish();
printf(" %i ", ofGetFrameNum());
for(int i=0; i<NUM_THREADS; i++) printf(" %i ", thread[i].getCounter());
printf(" **** ]");
}
**EDIT ** the lock() and unlock() simply do pthread_mutex_lock and unlock