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

Ivan Maidanski ivmai at
Wed Jun 23 22:30:44 PDT 2010

Wed, 23 Jun 2010 23:47:09 +0200 Henning Makholm <makholm at>:

> > 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?

I'd recommend you start with gc-7.2alpha4.tar.gz. The client sample code is:

#define GC_THREADS
#include "gc.h"

int main(void) {
  handle = _beginthreadex(NULL, 0, routine_to_start, param, 0, &thread_id);

For compiling GC for MinGW (multi-threaded), I personally use the following command:

gcc -O2 -fno-strict-aliasing -fno-omit-frame-pointer -Wall -DALL_INTERIOR_POINTERS -DNO_DEBUGGING -DGC_THREADS -DUSE_MUNMAP -DTHREAD_LOCAL_ALLOC -DPARALLEL_MARK -I include -I libatomic_ops\src -c *.c *.cpp
ar crus libgcmt.a *.o

Or, simply, if leaving out any optimization:

ar crus libgcmt.a gc.o gc_cpp.o

> I compiled gc with the configure arguments:
> --enable-threads=win32 --enable-thread-local-alloc --enable-parallel-mark

This should also work for you. (Another build system supported is CMake.)

> 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
> <windows.h>

As for v7.2, there's no restriction for windows.h inclusion place (before or after).

> 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.

As for v7.2, _beginthreadex is wrapped by GC (the same as CreateThread()) and, of course, no need to patch win32_threads.c anyhow.

> --
> Henning Makholm
> Octoshape ApS


More information about the Gc mailing list