3

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

ulrichb
  • 19,610
  • 8
  • 73
  • 87

2 Answers2

4

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.

Ondergetekende
  • 1,065
  • 9
  • 22
  • OK. But I cannot really find a call to `g_hook_insert_sorted()` inside *gmain.c* (I added a link in the question)? – ulrichb Feb 10 '10 at 15:28
0

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);
gcbenison
  • 11,723
  • 4
  • 44
  • 82