Re: g_main_context_push_thread_default() & g_io_*()



On Mon, Mar 29, 2010 at 6:58 PM, Thomas Stover <thomas wsinnovations com> wrote:
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.

GIO refers to the GIO library
(http://library.gnome.org/devel/gio/stable/), not to the g_io_* family
of functions. Probably worth filing a bug to make this clearer in the
documentation.

-A. Walton


====
#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
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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