[gameprogrammer] pthreads question

  • From: Alan Wolfe <alan.wolfe@xxxxxxxxx>
  • To: gameprogrammer@xxxxxxxxxxxxx
  • Date: Thu, 19 Apr 2012 15:25:19 -0700

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: