应用程序后台时线程被杀死?

Thread killed when application is backgrounded?

我正在开发一个 fuse 文件系统,在调用 fuse_main_real() 之前,我在 class 实例中启动了一个 std::thread 来做一些后台工作。

this->t_receive = new std::thread([this] { this->receive(); });

但是,似乎 fuse_main_real() 在进入后台时终止了该线程。
如果我使用 -f 选项启动程序,它告诉 fuse 留在前台,问题就不会发生,线程仍然存在。

我不确定什么保险丝会进入后台。

如何让我的线程在后台运行?


编辑:这是一个暴露问题的基本示例:

#define FUSE_USE_VERSION 30

#include <iostream>
#include <thread>

#include <fuse.h>

static void thread_method()
{
        while (true)
        {
                std::cout << "test" << std::endl;
                std::this_thread::sleep_for(std::chrono::seconds(1));
        }
}

struct testop : fuse_operations
{ };

static struct testop ops;

int main(int argc, char** argv)
{
        std::thread(thread_method).detach();
        fuse_main_real(argc, argv, &ops, sizeof(fuse_operations), NULL);
}

编译

g++ -D_FILE_OFFSET_BITS=64 `pkg-config --cflags fuse` -lfuse -lpthread -std=c++14 -o test.o test.cpp

有效的命令(重复说 "test"):

./test.o -f /home/test/testmountpoint

命令不起作用并显示问题(说 "test" 一次):

./test.o /home/test/testmountpoint

libfuse wiki

Miscellaneous threads should be started from the init() method. Threads started before fuse_main() will exit when the process goes into the background.