Any chance for Thread::join()?


#1

Hi,

The other day I discovered that Thread::waitForThreadToExit() calls sleep() internally. I discovered this by way of crash, where sleep() would return at a time the Thread object had already disappeared. And, digging a little deeper, I also found that threads are created detached, making joining impossible. Since I have used [p]thread_join() (or WaitForXYZ() on Windows) quite happily in a variety of languages, I am wondering why JUCE does not expose the join functionality? Using sleep() in a thread primitive certainly seems brittle.

Thank you!


#2

I don’t really see why the internal implementation matters…? Agreed that I could use tricks to do a slightly more efficient job than sleeping, but if you find yourself hitting a performance issue with waitForThreadToFinish, then you need to be using a different idiom like a thread pool or TaskThread, or futures, etc. Starting and stopping threads is always inefficient and messy, even if done optimally, and should be avoided.

Then how would join() help? You’re in charge of managing the lifetime of the Thread object, so if your code deletes it when another thread is still trying to use it, that’s not the fault of the class itself.