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

Adam Coates acoates at stanford.edu
Wed Sep 19 23:29:51 PDT 2007

So, I just realized that the OSG folks are not synonymous with the
OpenThreads folks.  ^_^  Sorry for dropping this on the wrong list
then.  In that light, it may not even be considered a bug in OT,
since, theoretically, it might be unfair to call the Thread destructor
while the thread is running (even though I'm pretty sure they intended
isRunning() to work as we'd expect).

It should be possible to add a workaround in the following way:

1.  Before starting the thread, set a flag to indicate that the thread
is "starting up".
2.  Have the thread's run() routine unset this flag.

Before deleting the Thread, make sure that both the "starting up" flag
and the isRunning flag are false.  One of these will have to be true
if the thread has not reached the run() method yet, or has not
completely exited from run() -- thus avoiding the problem.

My question:  does somebody want me to implement this workaround and
submit it to you?  Alternatively, I can just point out the break to
the OpenThreads people and we can wait for a patch...


On 9/19/07, Adam Coates <acoates at stanford.edu> wrote:
> 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,
> AC

More information about the osg-users mailing list