GNOME Bugzilla – Bug 149490
Need to call g_thread_init when using message logging and threads
Last modified: 2004-12-22 21:47:04 UTC
When you use message logging (via g_log et al) and you are using threads, things don't work well unless you called g_thread_init(). This is not a problem if you're using gthread (because then, you'll call g_thread_init), but if someone don't use gthread and directly uses pthread, then he might find a crash like this if he uses g_log from two threads: "(process:608): MyLogDomain-LOG-0x100 (recursed): my log message aborting..." As explained in [1]: 'the "(recursed)" part above indicates that g_log() was called from within a handler of g_log() within the same thread.' The problem here is probably that glib doesn't seem to know that there are multiple threads. Adding g_thread_init (NULL) make it work. This should probably be written down somewhere in the docs. [1] http://mail.gnome.org/archives/gtk-devel-list/2001-June/msg00446.html
Created attachment 30275 [details] Small test case This is a small test case to see the crash.
I guess I'm just misunderstanding something abou thtis bug report.... you tried to use GLib from a threaded app without initializing threads and didnt' work... There's nothing different about g_log() than any other GLib function.
I'm just saying that people might not use GThread but directly pthread. If they use pthread, how should they know that they have to call g_thread_init()? If I'm using threads, should I always call g_thread_init()? Even if I don't use any g_thread_*() function? From the documentation of g_thread_init(): "Before you use a thread related function in GLib, you should initialize the thread system. This is done by calling g_thread_init()." But if I just use g_log*() functions (as in the sample I attached) and no other GLib functions, I don't use any thread related function in GLib. Or maybe I'm not correctly understanding "thread related function in GLib"? (you may say that people using threads and GLib should use GThread, but as far as I understand it, it's not an obligation and just a recommendation)
OK, that g_thread_init() comment needs to be changed to "If you use GLib from more than one thread, you must initialize the thread system by calling g_thread_init()" All sorts of parts of GLib need locking to be initialized to work in a threaded application.
Changed the docs.