[Gc] multi-threading problems using gc in a win32 mingw environment

Henning Makholm makholm at octoshape.com
Wed Jun 23 14:47:09 PDT 2010

> I'm trying to use the gc in an windows multi-threading application using
> the mingw gcc compiler and the glib library for multi threading.
> gc is working fine as long as there is just one thread running. At some
> point when trying to allocate memory in a second thread (not the first
> time i do so in that thread)  i get the error: Fatal Error in gc:
> Collecting from unknown thread.

Which version of the GC are you using? How are you creating your thread?

With version 6.7 (no longer current, but MAY still be the case - caveat
emptor!), the gc.h header tries to redirect calls to CreateThread to a
wrapper function that takes care of registering the new thread in the GC
structures. This only works if you #include <gc.h> in the source file
where the CreateThread is, and this include must come after that for

Also, this wrapping does not work at all if you use _beginthreadex from
the Microsoft C runtime instead of CreateThread (which MSDN says one ought
to because otherwise certain unspecified CRT functions may crash). In my
application I solved this by patching win32_threads.c to make
GC_new_thread and GC_delete_thread nonstatic, and then calling them
explicitly from my thread main function. YMMV as to whether that is
a path you want to go down.

Henning Makholm
Octoshape ApS

More information about the Gc mailing list