Way to determine if I am running on main fltk's thread

59 views
Skip to first unread message

Gonzalo Garramuño

unread,
Feb 28, 2021, 8:46:57 AM2/28/21
to fltkg...@googlegroups.com
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...
}

lifeatt...@gmail.com

unread,
Feb 28, 2021, 10:04:35 AM2/28/21
to fltk.general
I believe the "correct" solution is to use Fl::awake() to tell the main thread to perform some operation.

If you can use C++, then you could track thread ids yourself using 
#include <thread>

std::thread::id main_thread;
main()
{
main_thread = std::this_thread;
}

func()
{
if (std::this_thread == main_thread) 
{
    win.show();

imm

unread,
Feb 28, 2021, 10:41:17 AM2/28/21
to general fltk
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!


Greg Ercolano

unread,
Feb 28, 2021, 11:12:37 AM2/28/21
to fltkg...@googlegroups.com


On 2/28/21 7:41 AM, imm wrote:
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?

    Seems like a non-FLTK question.. this has more to do with the thread library you're using,
    e.g. pthreads.


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!

    I googled for 'pthreads determine main thread'..  seems it might be platform specific:
    https://stackoverflow.com/questions/4867839/how-can-i-tell-if-pthread-self-is-the-main-first-thread-in-the-process

    I imagine if pthread_self() is the way to get the current thread ID, then just save that
    value early in your main(), before you start any child threads, and save the value to
    a global, then compare that global to pthread_self() later in your code, e.g.

// main.c
pthread_t G_main_tid;   // GLOBAL: main thread's ID
..

int main() {
    ..
    G_main_tid = pthread_self();   // save main thread's ID
    ..
}

void some_function() {
    if ( G_main_tid == pthread_self() ) {
        // main thread
    } else {
        // child thread
    }
}

Gonzalo Garramuño

unread,
Feb 28, 2021, 3:51:03 PM2/28/21
to fltkg...@googlegroups.com

El 28/2/21 a las 12:04, lifeatt...@gmail.com escribió:
> I believe the "correct" solution is to use Fl::awake() to tell the
> main thread to perform some operation.
>
> If you can use C++, then you could track thread ids yourself using
> #include <thread>
>
> std::thread::id main_thread;
> main()
> {
> main_thread = std::this_thread;
> }
>
> func()
> {
> if (std::this_thread == main_thread)
> {
>     win.show();
> }
> }
Oh yes!  std::thread::id!  Had completely forgotten about it.  One gets
so immersed in fltk that forgets that the rest of C++ exists :D

janezz55

unread,
Mar 2, 2021, 5:53:42 AM3/2/21
to fltk.general

> std::thread::id const main_thread_id(std::this_thread::get_id()); 

 I think this would be better.
Reply all
Reply to author
Forward
0 new messages