[Gc] pthreads and libgc

Boehm, Hans hans.boehm at hp.com
Sat Mar 19 19:51:05 PST 2011

> From: Andy Wingo
> ...
> [From Ivan:]
> > But, anyway, it is recommended to initialize GC explicitly (i.e. by
> > GC_INIT), so placing GC_INIT() (together with
> > GC_set_all_interior_pointer) to the beginning of your main() should
> be
> > the best way out.
> We can add some documentation to this regard in the manual, if needed.
> But is there no way to get around this, and do the right thing?  For
> example, to avoid implicit GC initialization in response to a
> pthread_create.
The problem is that GC_pthread_create needs to allocate memory, and relies on some other GC state.  Hence the GC must have been initialized for it to work.

For newer collectors, it might be possible to instead arrange for pthread_create not to be redirected to GC_pthread_create, and for threads that need to be known to the GC to register themselves.  I haven't thought through whether that might be feasible here.

Another imperfect solution might be to set all_interior_pointers and probably call GC_INIT in a constructor, either a C++ constructor, or a C function that's suitably attributed.  That still fails if other constructors run into this problem before our constructor runs.


More information about the Gc mailing list