[osg-users] OpenThreads threading bug on creation/deletion (leads to crashes)

Adam Coates acoates at stanford.edu
Wed Sep 19 20:49:35 PDT 2007

I ran into a crash caused (apprently) by OpenThreads (pthread
implementation).  If you create a new thread, and then immediately
destroy it, it is possible to delete the thread before it has been
flagged as "running".  Thus, whoever in osgViewer is waiting on the
thread to close ends up deleting the thread, never realizing that it
has already been started (resulting in a crash).

The OpenThread source (see Thread::~Thread() in Pthread.c++) suggests
that even attempting to delete a thread while it's running is an
error.  But it, too, uses the "isRunning" flag as its signal that the
thread is either running or not, so it doesn't even notice the error
when the thread gets destroyed.  isRunning is not actually a correct
indicator of whether the thread is running, as presently implemented.

I temporarily solved this by adding:  pd->isRunning = true;  just
before pthread_create() in Thread::start().  This solves my problem,
but it should probably be looked at / solved by somebody more familiar
with the code.  Modifying asynchronous code you didn't write is a bad
bad feeling... =(

This is in the OpenSceneGraph-2.1.11 SVN source.

Thanks all,

More information about the osg-users mailing list