More mac and linux -- parallelization
I had written a renderer that used pthread and semaphores to partition and then manage the job, so I thought I'd see if I could do the same thing on my mac. Unfortunately, I had used unnamed semaphores (sem_init(...)) on my Linux boxes, and the program failed (all threads appeared to be finished immediately) on the Leopard box.
Turns out that when you examine the error return, sem_init() has an entry point and prototype in the library and header file respectively, but it's actually "Not Implemented". So, I switched over to named semaphores, and it all works perfectly.
In particular, on my 8-core box, I was getting some 6x speedup.
// start the threads
for(thread_id = 0; thread_id < n_proc; thread_id++) {
char sem_name[64];
printf("Starting thread %d\n", thread_id);
sprintf(sem_name, "sem_%d\n", thread_id);
if((semaphore[thread_id] = sem_open(sem_name, O_CREAT, 0777, 0)) == SEM_FAILED) {
printf("sem_open failed\n");
perror("sem_open error: ");
return -1;
}
ret = pthread_create(&thread[thread_id], NULL, (void *)median_scanlines, (void *)thread_id);
}
// wait for all threads to finish -- when thread is done, it calls sem_post(), then sem_wait below will unblock
for(thread_id = 0; thread_id < n_proc; thread_id++) {
printf("Waiting for thread %d to finish\n", thread_id);
sem_wait(semaphore[thread_id]);
}