I have some misunderstanding about how GMainLoop
work.
Main thing - API which add some callbacks into g_main_loop
(like g_timeout_add_seconds()
) don't take pointer to which loop you want to add that callback.
It looks like you add callback's for all g_main_loop
instances.
Even if you have not yet created. Simple example for this:
#include <glib.h>
gboolean callback(gpointer data)
{
static guint16 i=0;
g_print("Iter=%"G_GUINT16_FORMAT"\n",i++);
if(i%5==0){
g_print("try to stop loop1\n");
g_main_loop_quit((GMainLoop*)data);
}
return TRUE;
}
int main()
{
GMainLoop* loop1 = NULL;
GMainLoop* loop2 = NULL;
loop1 = g_main_loop_new (NULL, FALSE);
g_timeout_add_seconds(1, callback,loop1);
loop2 = g_main_loop_new (NULL, FALSE);
g_print("run loop1\n");
g_main_loop_run(loop1);
g_free(loop1);
g_print("run loop2\n");
g_main_loop_run(loop2);
g_free(loop2);
return 0;
}
Result is:
run loop1
Iter=0
Iter=1
Iter=2
Iter=3
Iter=4
try to stop loop1
run loop2
Iter=5
Iter=6
Iter=7
Iter=8
Iter=9
try to stop loop1
Segmentation fault (core dumped)
Is it possible add callback()
to loop1
, and don't add it to loop2
?