【发布时间】:2020-03-03 12:38:53
【问题描述】:
在多线程程序中使用Boehmgarbage 收集器时遇到问题。
我的主函数休眠,而线程正在使用垃圾收集器执行一些分配和释放。
当垃圾收集器调用collect()时,主线程的睡眠被中断,程序继续进行,就像什么都没发生一样。
以下源代码在 1 秒后终止,而它必须至少休眠 100 秒:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#define GC_THREADS
#include <gc/gc.h>
void foo () {
sleep (1);
GC_gcollect (); // or multiple allocation, that will trigger a collect at some point
}
void * thread_func (void* data) {
foo ();
}
int main () {
// GC_init (); ordinarily useless, and does not change anything
pthread_t id;
GC_pthread_create (&id, NULL, &thread_func, NULL);
sleep (100);
printf ("End \n");
}
同样的问题发生在线程处于休眠状态和执行分配的主函数时。
我在ubuntu-18.04 上使用bohem gc 的最后一个稳定版本(即8.0.4)。
有人知道发生了什么吗?
【问题讨论】:
-
我打赌 sleep 返回错误代码 EINTR。
-
@Emile Cadorel - 你的意思是“Boehm GC”吗?
-
如果您使用 C 编译器而不是 C++ 编译器进行编译,我认为您实际上应该调用 GC_init()。但我不明白为什么 sleep() 不起作用。 sleep() 返回什么?
-
是的,对不起 Boehm GC。该函数返回值
98,所以不是EINTR,它等于4。98是 errno.h 中EADDRINUSE的值 -
sleep()返回98,errno 设置为4
标签: c multithreading garbage-collection