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 Greg Ercolano Feb 28, 2021  
 


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
    }
}

--
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/6f1a9f7e-50ce-a72d-fe38-6ed5560e5fb2%40seriss.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'.