Hello.
My UI involves lots of threads, and often call gdk_threads_idle_add to apply updates to UI.
Some of the calls need the result to be returned back,
so I let the calling thread listen to the mutex to be written by the idle task.
Problem is, main loop will deadlock if I accidentally call such function from the main thread. The calling main thread is waiting for main loop inside the main loop! I would like to detect such occasions so I could debug these cases easily.
In short, is there a way to detect if current thread is the main thread?
If you are passing messages between a worker thread and the main thread, you should probably have a GMainContext handle for each. For the main thread, this will be NULL. There should be a 1:1 correspondence between GMainContext instances and threads: each thread should have exactly one GMainContext. Any operation you’re running should probably have a pointer to the GMainContext it’s meant to run in or return its results to.
As such, you can check which GMainContext a function is running in by calling g_main_context_is_owner().
No matter what you do, you’ll come down to this, either directly by your application or a level below because there is no concept of “main thread” at the operating system level in Linux.
Your other option is to check if gettid() == getpid(), but this is Linux only (and there is no syscall wrapper for gettid().
GNOME Builder sets a global to g_thread_self() at startup and asserts everywhere we expect to be on the main thread to catch threading issues early and often.