On Sun, 28 Feb 2021, 13:46 Gonzalo Garramuño wrote:
Is there a function to determine whether a piece of code is running on
the main fltk's thread?
Something like:
if ( Fl::main_thread() )
{
win.show();
}
else
{
// do nothing...
}
Hi Gonzalo,
I hit a similar sort of issue a year or so back, where I had some common callbacks that could be invoked either from the main thread or from one of the workers.
(The design wasn't mine, I was "fixing" a body of existing code that was crashing sporadically...)
Anyway, I "adjusted" the code so that in the main thread the callbacks ran normally, but in the worker threads they used the FL::awake(...) mechanism, and that resolved the problem.
But; how did I determine whether it was the main thread? I can't remember...
I *think* I stored the thread ID of the main thread in main() as soon as the app started and had a global method to allow that to be read.
Then each CB checked that against the current thread ID when it ran, to make the choice.
Under Win32 that's a call to GetCurrentThreadID(), can't remember the pthread equivalent off the top of my head, but it seemed to work anyway!