BoehmGC + pthread

Boehm GC をマルチスレッド環境で使うときのメモ.

include 方法

#define GC_THREADS // pthread_*とかを上書きする
#include "gc.h"
#include 
#include 
#include 
http://fistfvck.sakura.ne.jp/w/c_cplusplus/boehmgc
  • GC_MALLOC とか普通にコールすればいい.
  • #inlucde "gc.h" より前に GC_THREADS を define
  • #inculde より前に "gc.h" を include

仕事でテスト辺りが今のメイン作業なので、気晴らしに家でプログラムを作っている。


それで、ここ数日悩んでいたのが、Pthreadを使っている環境でBoehm GCのGC_malloc()を使うとSIGSEGVが発生する現象。


ネットで調べても解決しないかったので、解決した方法を書いておく。


細かく見ていくと、スレッドを使ってもmain()では発生せずにスレッド内でGC_malloc()を行うとダメらしい。


(中略)


その中に、GC_init()といういかにもな関数があった。


コメントにもあるようにスレッドを使うときには必要っぽい。

英語が苦手なのでこれぐらいしか分からないけど。

/* Initialize the collector.  This is only required when using thread-local 
 * allocation, since unlike the regular allocation routines, GC_local_malloc
 * is not self-initializing.  If you use GC_local_malloc you should arrange   
 * to call this somehow (e.g. from a constructor) before doing any allocation.
 * For win32 threads, it needs to be called explicitly.
 */
GC_API void GC_init(void);

ということで、GC_init()を明示的に呼んでやることで無事解決。

2007-11-24

だいぶ前だから今はいらない?よくわからない.

GDB

(gdb) help handle # ヘルプ表示
(gdb) handle SIGPWR "nostop" "pass" "noprint"
(gdb) handle SIGXCPU "nostop" "pass" "noprint"

SIGPWR や SIGXCPU は Boehm GC 関連で本来の用途とは違う用途で使われている。

GDB で特定のシグナルを無視したり表示したり - higepon blog

これやらないと gdb で動かすとすぐに止まってしまう

Program received signal SIGPWR, Power fail/restart.
[Switching to Thread 0xb6f5bb90 (LWP 17644)]
0xb777d410 in __kernel_vsyscall ()