I found myself in need of launching a lambda in its own Thread object, exactly like Thread::launch() does, but still having a Thread object to check for termination, etc.
I was about to wrap my own when noticed that Thread::launch() makes use of the following class defined in the cpp:
struct LambdaThread : public Thread
{
LambdaThread (std::function<void()> f) : Thread ("anonymous"), fn (f) {}
void run() override
{
fn();
fn = nullptr; // free any objects that the lambda might contain while the thread is still active
}
std::function<void()> fn;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LambdaThread)
};
that would perfectly suit my needs, any chance to have it exposed for public use?
I don’t see any drawbacks and that would prevent some wheel reinvention, thanks.
No, my need is to call waitForThreadToExit() on the spawned thread from the main thread, because I need the timeout that comes with it and the information regarding whether it succeeded in time or not.
a second option would be to have an overload of Thread::launch() that does exactly that, with a timeout parameter
I’m sorry, but… what appears to be the drawback of exposing LambdaThread?
To me it looks like a useful class that is also very tidy and simple, not a mere “implementation detail” subject to future fiddling that needs to be hidden.
If deleteOnThreadEnd was a feature publicly available, I would have rewritten that class for my usage exactly like that.