[g_deb at zewt.org: Re: [SDL] Problem with threads and virtual functions]

Glenn Maynard g_sdl at zewt.org
Mon Nov 17 18:37:01 PST 2003

I'm posting this one on the list since the answer to his question is
somewhat more obscure than it looked.  I'll take any further replies

(Oops, posted with the wrong address ...)

On Mon, Nov 17, 2003 at 05:56:21PM -0600, Tyler Montbriand wrote:
> I'm just creating
> threads...  the pointer is not dereferenced by the thread until long after
> the constructor returns.  Indeed, the semaphore is there to guarantee that.

I looked at this a bit more.  The real problem is the other part of
the same issue: when you get to the dtor of a parent class, the derived
class no longer exists--it's reverted to the underived state it was in
when the ctor was called.

So, you have a race condition: any ThreadFuncs called after mtclass::Go
calls Launch and before main() returns (and calls testthread::~testthread)
will call the derived class.  Otherwise, testthread no longer exists; your
"t" object is only an mtclass, and the rest of the ThreadFunc calls that
are being flushed while mtclass::~mtclass blocks on the mutex will call

I was seeing two Woos and a Narf, myself; probably because I'm running
Linux 2.6, which has a much better tuned scheduler.

Glenn Maynard

More information about the SDL mailing list