[gameprogrammer] Re: pthreads question

Ok nevermind.... i think i have this fixed after all

I'm not sure why that solution wasn't working, but my new solution is a lot
cleaner and has less moving parts which is nice...

Main Thread:
* calls pthread_cond_broadcast on a single "Thread Wake Up" event to wake
up all the threads
* calls pthread_sem_wait on a single "Thread Back To Sleep" semaphore once
per thread that is running, to wait for all the threads to be finished
working

Thread Func:
* While(1)
  * calls pthread_cond_wait on the global "Thread Wake Up" event to sleep
until the main thread tells it to wake up
  * Does rendering work
  * calls pthread_sem_post on the global "Thread Back to Sleep" semaphore
to tell the main thread it's done rendering


On Thu, Apr 19, 2012 at 3:25 PM, Alan Wolfe <alan.wolfe@xxxxxxxxx> wrote:

> Hey Guys,
>
> I'm having a pthreads issue and was wondering if anyon could see anything
> wrong with what I'm doing.
>
> My situation is that I have a software rendered raytraced game on the
> google NaCl.  The screen is broken up into a grid and the cells are
> rendered by worker threads.
>
> There's a point in the game loop where the main thread wakes up the worker
> threads, then is supposed to go back to sleep until all the worker threads
> are done with their work.
>
> Here's my latest incarnation of efforts, which usually hangs after a
> couple seconds at ~50fps.
>
> Init Time:
> * for each thread, a "wake up" and "back to sleep" semaphore is created
> and they are both initialized to a count of zero
> * the threads are launched
>
> Main Thread:
> * When the game loop gets to the rendering portion, it does a sem_post to
> each thread's "wake up" semaphore in a for loop. (supposed to wake up all
> the threads)
> * Then it does a sem_wait on each thread's "back to sleep" semaphore, in a
> second for loop. (supposed to make the main thread wait til all the threads
> are done)
>
> Thread Function:
> * casts it's thread params to the right struct pointer so it can get at
> it's own semaphores etc.
> * while(1)
>   * The thread does a sem_wait on it's own "wake up" semaphore (ie sleep
> til main thread notifies the thread)
>   * The thread does rendering work
>   * The thread does a sem_post on it's "back to sleep" semaphore (to
> notify the main thread that it's done)
>
> What do you guys think, do you see anything wrong there?
>
> does anyone know if sem_wait has spurious wakeups like condition variables
> do?  I was trying to google that but couldn't find anything that said one
> way or the other, so i'm assuming they don't but not sure :P
>

Other related posts: