我希望能够在 C++ 中的进程终止之前捕获主函数的结尾并在另一个线程中执行其他操作。
我一直在尝试获取主线程的句柄,然后加入它,但似乎 std::this_thread/boost::this_thread 不允许您访问当前线程的句柄。
我想做的基本上如下:
void thread_function(thread_handler) {
thread_handler.join();
< Perform extra operations before the program finishes its main function >
}
int main() {
< thread_function thread started in LD_PRELOAD>
.... Program runs .....
return 0;
}
在我的场景中,这两个函数没有共享状态,因为 thread_function 线程是在共享库中使用 LD_PRELOAD 调用的,但我可以从稍后调用main()的线程中传递任何东西。很可能不可能赶上主要功能的结尾,但我想如果有人知道这一点,我会问。