=?UTF-8?Q?g=5Fmain=5Fcontext=5Fpush=5Fthread=5Fdefault=28=29=20=26=20g=5F?= =?UTF-8?Q?io=5F*=28=29?=



In the documentation for g_main_context_push_thread_default(), the
following sentence appears:

"This will cause certain asynchronous operations (such as most gio-based
I/O) which are started in this thread to run under context and deliver
their results to its main loop, rather than running under the global
default context in the main thread."

This makes me think my experiment program below would work, yet on ubuntu
9.10 at least, it does not. Perhaps g_io_channel_unix_new() is not in the
"most" group referred to above? Or maybe I'm doing this wrong. Thanks for
any input.

====
#include <glib.h>
#include <unistd.h>
#include <sys/syscall.h>

GMainContext *thread1_context, *thread2_context;
GMainLoop *main_loop1, *main_loop2;

gboolean idle_callback(gpointer data)
{
 g_print("idle_callback() %d\n", (pid_t) syscall (SYS_gettid));
 return FALSE;
}

gboolean input_callback(GIOChannel *source, 
                        GIOCondition condition,
                        gpointer data)
{
 GSource *idle_source;
 unsigned char buffer[16];
 g_print("input_callback() %d\n", (pid_t) syscall (SYS_gettid));

 if(read(0, buffer, 16) < 1)
  return FALSE;

 idle_source = g_idle_source_new();
 g_source_set_callback(idle_source, idle_callback, NULL, NULL);
 g_source_attach(idle_source, thread1_context); 

 return TRUE;
}

gpointer thread2_entry(gpointer data)
{
 GIOChannel *channel;
 g_print("thread2_entry() %d\n", (pid_t) syscall (SYS_gettid));

 main_loop2 = g_main_loop_new(thread2_context, FALSE);

 g_main_context_push_thread_default(thread2_context);

 channel = g_io_channel_unix_new(0);
 g_io_add_watch(channel, G_IO_IN, input_callback, NULL);

 g_main_loop_run(main_loop2);
}

int main(int argc, char **argv)
{
 g_thread_init(NULL);

 thread1_context = g_main_context_default();
 thread2_context = g_main_context_new();

 main_loop1 = g_main_loop_new(thread1_context, FALSE);

 g_thread_create(thread2_entry, NULL, FALSE, NULL);

 g_main_loop_run(main_loop1);
 return 0;
}
====

Here is an example session:

$ ./a.out 
thread2_entry() 24928
f
input_callback() 24927
idle_callback() 24927
e
input_callback() 24927
idle_callback() 24927
k
input_callback() 24927
idle_callback() 24927
^C

What I was expecting is for input_callback() to run in thread 24928
instead of 24927.

-- 
www.thomasstover.com



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