Search code examples
c++clinuxglib

What is advantage of using GMainLoop from glib.h rather than "while(true);" in C++ linux?


I have come across a code where they needed an infinite loop & they used

    GMainLoop *mainloop = NULL;

    mainloop = g_main_loop_new (NULL, FALSE);
    g_main_loop_run (mainloop);

I have doubt why don't they use while(true); for same purpose. What does GMainLoop internally do, Ain't it will be same internally. How does GMainLoop enhances the performance.


Solution

  • g_main_loop means the main event loop in glib. It is not just an infinite loop; it polls event sources, queues events it gets from them, and invokes event handlers. It also doesn't do that busily; that is to say, it'll not go to 100% CPU usage when nothing is happening (unless an event source is broken).

    There's a description of it in the glib documentation.