[英]Problem with Boehm gc and mutli-thread program
在多线程程序中使用Boehm
垃圾收集器时遇到问题。
我的主函数休眠,而线程正在使用垃圾收集器执行一些分配和释放。
当垃圾收集器调用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
)。
有人知道发生了什么吗?
垃圾收集器在内部使用许多信号(根据调试文档、 SIGSEGV
、 SIGBUS
以及多线程 linux 设置上的SIGPWR
和SIGXCPU
),并为它们设置信号处理函数。
sleep()
将在调用信号处理程序时被中断,并返回如果未中断则超时之前剩余的秒数。 如果在睡眠中触发收集,就会发生这种情况。
因此,如果要将sleep()
与垃圾收集器混合使用,则必须使用如下循环:
int timeout = 100;
int time_remaining;
while ((time_remaining = sleep(timeout)) > 0) {
timeout = time_remaining;
}
一个更健壮的实现直接使用nanosleep()
(这是在 Linux+Glibc 上实现sleep()
方式)以获得更好的错误处理:
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);
}
}
一个更健壮的版本,由于垃圾收集器使用的时间,它的睡眠时间不会超过 100 秒(除非到目前为止睡眠所花费的时间 + gc 时间超过该时间)使用clock_nanosleep()
睡眠直到给定的时间戳之后:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.