使用 C++0x 线程库退出时崩溃

Posted

技术标签:

【中文标题】使用 C++0x 线程库退出时崩溃【英文标题】:Crash on exit using C++0x threading library 【发布时间】:2011-09-28 04:10:10 【问题描述】:

我刚开始尝试新的 C++0x 线程库,我终于得到了几个并行运行的计算任务。该示例需要一秒钟才能运行,这是我的预期,但它在退出时崩溃。知道我做错了什么吗?

#include <future>
#include <vector>
#include <iostream>

int main() 
    std::vector<std::thread> workingTasks;
    std::vector<std::future<int>> output;
    for (int i = 0; i < 6; ++i) 
        std::packaged_task<int()> pt(std::bind([](int data) sleep(1); return data*data;, i));
        output.push_back(pt.get_future());
        std::thread task(std::move(pt)); // launch task on a thread
        workingTasks.push_back(std::move(task));
    

    for (int i = 0; i < output.size(); ++i) 
        std::cout << i << ": " << output[i].get() << std::endl;
     

gdb回溯如下:

Program received signal SIGABRT, Aborted. 0x0000003e45e330c5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64 64        return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig); 
(gdb) bt
#0  0x0000003e45e330c5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1  0x0000003e45e34a76 in abort () at abort.c:92
#2  0x0000003e496bc08d in __gnu_cxx::__verbose_terminate_handler ()
    at ../../../../libstdc++-v3/libsupc++/vterminate.cc:93
#3  0x0000003e496ba2a6 in __cxxabiv1::__terminate (handler=<value optimized out>)
    at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:39
#4  0x0000003e496ba2d3 in std::terminate () at ../../../../libstdc++-v3/libsupc++/eh_terminate.cc:49
#5  0x0000000000402d71 in std::thread::~thread (this=0x612d50,
__in_chrg=<value optimized out>)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/thread:146
#6  0x0000000000407052 in std::_Destroy<std::thread> (__pointer=0x612d50)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:89
#7  0x0000000000406468 in std::_Destroy_aux<false>::__destroy<std::thread*> (__first=0x612d50, 
    __last=0x612d80)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:99
#8  0x00000000004053fd in std::_Destroy<std::thread*> (__first=0x612d50, __last=0x612d80)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:122
#9  0x0000000000404963 in std::_Destroy<std::thread*, std::thread> (__first=0x612d50,
__last=0x612d80)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_construct.h:148
#10 0x0000000000403caa in std::vector<std::thread, std::allocator<std::thread> >::~vector (
    this=0x7fffffffddd0, __in_chrg=<value optimized out>)
    at /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_vector.h:313
#11 0x0000000000401f9c in main () at main.cpp:18

【问题讨论】:

【参考方案1】:

我发现了问题。 std::thread 是一个非常低级的原语,必须加入()才能正常工作。在输出部分修复崩溃后添加以下代码:

for (int i = 0; i < workingTasks.size(); ++i) 
    workingTasks[i].join();

【讨论】:

为了绝对安全,您还必须保护异常路径(如果workingTasks 因展开而破坏,请选择加入或分离)。虽然在这个小例子中,异常会导致终止。

以上是关于使用 C++0x 线程库退出时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

C# Winforms 无异常从外部函数崩溃,退出代码为 40445582 (0x269268e)

为啥程序退出时全局或静态对象会导致崩溃?

线程以代码 0 (0x0) 退出,没有未处理的异常

退出清理线程

VS2013,C++:未知的“线程 X 以代码 0 退出”

多线程 (pthreads) MATLAB mex 函数导致 MATLAB 在退出后崩溃