DBus and GMainContexts

Samuel Cormier-Iijima sciyoshi at gmail.com
Thu Sep 14 15:00:40 PDT 2006

Hi everybody,

This is my first post to the list :-) Sorry if this is the wrong
place, is there another list for the GLib bindings specifically?
Anyways, I was having some problems using GMainContexts with DBus...
I've attached code that I *think* should work. Both timeouts get
called, but whenever I try calling a function from e.g. a Python
client, the client hangs (the server just keeps chugging out
timeouts). tracker_dbus_new creates an exported DBus object that works
when used with a single thread... Thanks for any help :-)

#include <glib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-bindings.h>

#include "tracker-dbus.h"

timeout (gpointer data)
	g_print ("timeout called from context %d\n", GPOINTER_TO_INT (data));
	return TRUE;

thread (gpointer data)
	DBusGConnection *connection;
	DBusGProxy *proxy;
	GMainContext *context;
	GMainLoop *loop;
	GSource *source;
	TrackerDBus *server;
	guint flags;

	context = g_main_context_new ();

	connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL);
	dbus_connection_setup_with_g_main (dbus_g_connection_get_connection
(connection), context);

	proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS,
	org_freedesktop_DBus_request_name (proxy, "testing.Tracker",

	server = tracker_dbus_new ();
	dbus_g_connection_register_g_object (connection, "/testing/Tracker",
G_OBJECT (server));

	source = g_timeout_source_new (1000);
	g_source_set_callback (source, timeout, GINT_TO_POINTER (2), NULL);
	g_source_attach (source, context);

	loop = g_main_loop_new (context, FALSE);
	g_main_loop_run (loop);

main (int argc, char *argv[])
	GMainLoop *loop;

	g_type_init ();
	g_thread_init (NULL);
	dbus_g_thread_init ();

	g_thread_create (thread, NULL, TRUE, NULL);

	loop = g_main_loop_new (NULL, FALSE);
	g_timeout_add (1000, timeout, GINT_TO_POINTER (1));
	g_main_loop_run (loop);

More information about the dbus mailing list