Re: main loop & asynchronous queues



Chris Vine wrote:
In the case of callbacks to execute in the default program loop/context
(the "main" main loop) a worker thread can use g_idle_add()/
g_idle_add_full().  To execute a callback in other threads' main loops
you will have to do it in long-hand, making a GSource object with
g_idle_source_new(), and attach it to the GMainContext for the thread
in question (it is relatively trivial).
Wow that might open up some doors, but I've got to understand this first. I'm fuzzy on several things. Here is some stabbing in the dark:

GMainLoop *thread1_mainloop, *thread2_mainloop;
GThread *thread2;
GSource *thread2_idle_source;

gboolean thread2_idle_function(gpointer data)
{
...
}

gpointer thread2_entry(gpointer data)
{
thread2_mainloop = g_main_loop_new(NULL, FALSE);
thread2_idle_source =  g_idle_source_new();
g_idle_add(thread2_idle_function, NULL);

g_main_loop_run(thread2_mainloop);
return NULL;
}

some_callback_in_thread1()
{
tell_thread2_idle_function_to_run_somehow();
}

main()
{
g_thread_init();
thread2 = g_thread_create(thread2_entry, NULL, FALSE, NULL);
thread1_mainloop = g_main_loop_new(NULL, FALSE);

establish_some_callback_in_thread1_to_be_called_at_some_point();

g_main_loop_run(thread1_mainloop);
}

So now what?



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]