Question

If I compile and run the code as-is, the process will run with 1 thread. If I uncomment the commented out section and then compile and run it, it runs with 2 threads.

I am compiling the code with this command: g++ pkg-config gtkmm-2.4 --cflags --libs test.cpp

When the program is running I can check how many threads are created with: ps -mC a.out

If I look at the second thread in ddd, I can see that it is running g_main_loop_run. This confuses me:

  • What is the purpose of this thread?
  • Why does adding a toolbar button create a new thread?
  • I thought g_main_loop_run() should only ever run in one thread (unless you use the GDK_THREADS_ENTER/GDK_THREADS_LEAVE macros). Since I am running Gtk::Main::Run() in my main thread am breaking the rules?

Thanks in advance for any help. It's driving me crazy.

#include <gtkmm.h>

bool OnDeleteEvent(GdkEventAny* PtrGdkEventAny)
{
    Gtk::Main::quit();
    return(true);
}

void OnExecuteButtonClicked()
{
}

int main(int argc, char *argv[])
{
    new Gtk::Main(0, NULL);

    Gtk::Window *ptrWindow = new Gtk::Window;
    ptrWindow->signal_delete_event().connect(sigc::ptr_fun(&OnDeleteEvent));

    /*
    Gtk::Toolbar *ptrToolBar = manage(new Gtk::Toolbar);
    Gtk::ToolButton *ptrToolButton;

    ptrToolButton = manage( new Gtk::ToolButton(Gtk::Stock::EXECUTE));
    ptrToolBar->append(*ptrToolButton, sigc::ptr_fun(&OnExecuteButtonClicked));

    ptrWindow->add(*ptrToolBar);
    */

    ptrWindow->show_all();
    Gtk::Main::run();

    return (0);
}
Was it helpful?

Solution

Sometimes GThreads are created when you use functions that rely on async behaviour. These usually create a GTask internally (with g_task_run_in_thread and friends) and run the synchronous version in a seperate thread (except for those being nativly async or async-able, those usually won't spawn another thread). Usually this is IO (i.e. GtkBuilder), Socket and IPC (dbus) related - so mostly glib stuff.

There might also be occasions which I am not aware of, that will spawn additional threads, the mainloop itself is strictly single threaded.

So in your case I can only think of two thing that could trigger this: The Stock image that is loaded from your local disk or the styling information of your theme.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top