Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the GTK+ timeout callbacks be called in strict time order?

When I add many different timeouts (with each intervall==0) in a thread, which is not the main thread (where gtk_main() resides)...

g_timeout_add(0, func, NULL);

... will then the different func() callbacks occur in the same order I called the corresponding g_timeout_add()'s?


The reason I'm asking is because GTK# is using internally timeouts to implement Application.Invoke() (see Application.cs and Timeout.cs).


EDIT: The concerning glib files are

  • http://git.gnome.org/browse/glib/tree/glib/gmain.c
  • http://git.gnome.org/browse/glib/tree/glib/ghook.c
like image 628
ulrichb Avatar asked Oct 19 '25 09:10

ulrichb


2 Answers

Internally, g_timeout_add calls g_hook_insert_sorted. If g_timeout_add_full is used, the priority determines the ordering, otherwise the hook is added at the end of the list. Hooks are executed in order, so when only g_timeout_add is used, the answer is yes.

Unfortunately, there is not no explicit guarantee, and to me, it looks like an implementation detail which might change in the future.

like image 150
Ondergetekende Avatar answered Oct 21 '25 23:10

Ondergetekende


How about enforcing the order of the calls by storing your callbacks explicitly in a list, then using a single g_timeout_add() to call a function that iterates over that list?

static gboolean
call_in_order (GList* callbacks)
{
  for (; callbacks != NULL; callbacks = callbacks->next)
    {
      g_assert (callbacks->data != NULL);
      GSourceFunc callback = (GSourceFunc)(callbacks->data);

      callback(NULL);
    }
}


...

 GList *callbacks = NULL;
 callbacks = g_list_append(callbacks, func1);
 callbacks = g_list_append(callbacks, func2);
 callbacks = g_list_append(callbacks, func3);
 callbacks = g_list_append(callbacks, func4);

 g_timeout_add(0, (GSourceFunc)call_in_order, callbacks);
like image 33
gcbenison Avatar answered Oct 22 '25 00:10

gcbenison