Create multiple UI threads or main threads simultaneously within a single application

I want to write a Java application using GTK for providing GUI in Windows OS.

Does GTK 4 provide support for the creation and management of multiple UI threads or main threads simultaneously within a single application?

I referred here which says it is not possible. Is my understanding correct ?

Can Glib helps creating and managing multiple main threads which are independent of each other?

I see that win32 APIs provide such possibility

The answer is no. Only one thread can be the GTK main thread. That’s usually the OS level main thread, but for a Java application you’ll probably want it to be the Java main thread (the one that runs the Java main). But it can be whatever thread you prefer, as long as there’s only one. Never use GTK on any other thread.

GLib doesn’t really have a concept of main thread. Use however many threads you want. You’ll want to review the GMainContext tutorial. Just remember that GLib objects are not threadsafe unless documented otherwise, so they cannot be passed between different threads without synchronization.

1 Like

You can create any thread you want; what you cannot do is use GTK API from multiple threads: only the thread that initialised GTK and started the main loop can use the GTK API. This is an immutable fact of how the graphics subsystem works.

If you have a long running operation running inside a thread that needs to update the UI you can schedule that update using the main loop, for instance using:

  • g_main_context_invoke()
  • g_idle_add_once()
  • g_timeout_add_once()
1 Like

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.