Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Boehm gc和多线程程序的问题_C_Multithreading_Garbage Collection - Fatal编程技术网

Boehm gc和多线程程序的问题

Boehm gc和多线程程序的问题,c,multithreading,garbage-collection,C,Multithreading,Garbage Collection,在多线程程序中使用Boehm垃圾收集器时出现问题。 当线程使用垃圾收集器执行一些分配和释放时,我的主函数处于休眠状态 当垃圾收集器调用collect()时,主线程的睡眠被中断,程序继续运行,就像什么也没发生一样 以下源代码在1秒后终止,但必须至少休眠100秒: #包括 #包括 #包括 #定义GC_线程 #包括 void foo(){ 睡眠(1); GC_gcollect();//或多个分配,将在某个点触发收集 } void*线程函数(void*数据){ foo(); } int main(){

在多线程程序中使用
Boehm
垃圾收集器时出现问题。
当线程使用垃圾收集器执行一些分配和释放时,我的主函数处于休眠状态

当垃圾收集器调用
collect()
时,主线程的睡眠被中断,程序继续运行,就像什么也没发生一样

以下源代码在1秒后终止,但必须至少休眠100秒:

#包括
#包括
#包括
#定义GC_线程
#包括
void foo(){
睡眠(1);
GC_gcollect();//或多个分配,将在某个点触发收集
}
void*线程函数(void*数据){
foo();
}
int main(){
//GC_init();通常是无用的,并且不会改变任何东西
pthread_t id;
GC_pthread_create(&id,NULL,&thread_func,NULL);
睡眠(100);
printf(“End\n”);
}
同样的问题也会发生,当线程处于休眠状态,并且是执行分配的主函数时。 我在
ubuntu-18.04
上使用了上一个稳定版本的
bohem gc
(即
8.0.4


有人知道发生了什么事吗

垃圾收集器在内部使用大量信号(根据、
SIGSEGV
SIGBUS
,以及在您正在使用的多线程linux设置上使用
SIGPWR
SIGXCPU
),并为它们设置信号处理程序函数

调用信号处理程序时,
sleep()
将被中断,并返回如果不中断,它将在超时之前剩余的秒数。如果在睡眠中触发采集,则会发生这种情况

因此,如果要将
sleep()
与垃圾收集器混合使用,必须使用如下循环:

int timeout = 100;
int time_remaining;
while ((time_remaining = sleep(timeout)) > 0) {
  timeout = time_remaining;
}
更健壮的实现直接使用(
sleep()
是如何在Linux+Glibc上实现的)来更好地处理错误:

struct timespec req = { .tv_sec = 100, .tv_nsec = 0 };
struct timespec rem;
while (nanosleep(&req, &rem) < 0) {
  if (errno == EINTR) {
    // Interrupted by a signal handler
    req = rem;
  } else {
    // Some other error happened; handle appropriately for your application
    perror("nanosleep");
    exit(EXIT_FAILURE);
  }
}

我敢打赌,睡眠会返回错误代码eTIN。@埃米尔卡多尔-你是不是说“BeHM GC”?我认为你应该调用GCGIN(),如果你用C编译器编译而不是C++编译器。但是我不明白为什么sleep()不起作用。sleep()返回什么?是的,对不起。该函数返回值
98
,因此不返回等于
4
的EINTR
98
是errno.h
sleep()
EADDRINUSE
的值,返回
98
,errno设置为
4
struct timespec req;
if (clock_gettime(CLOCK_MONOTONIC, &req) < 0) {
  perror("clock_gettime");
  exit(EXIT_FAILURE);
}
req.tv_sec += 100;
int rc;
while ((rc = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &req, NULL)) != 0) {
  if (rc != EINTR) {
     fprintf(stderr, "clock_nanosleep: %s\n", strerror(rc));
     exit(EXIT_FAILURE);
  }
}