FLTK logo

Re: [fltk.general] Way to determine if I am running on main fltk's thread

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Way to determine if I am running on main fltk's thread imm Feb 28, 2021  
 
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!


--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/CAGFM6dY5BnafYdH7jvKeTZXvQfhStTSRc3nAcj7nBaX8PPtdbA%40mail.gmail.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.