c++ - g++4.8 处理 vector future 的问题

标签 c++ g++ future

这是我的问题的简单再现:

#include <vector>
#include <future>

using namespace std;

typedef unsigned Counter;
typedef vector<Counter> Counters;

Counters computer(int n)
{
    Counters result(n, 0);
    return result;
}

int main(void)
{
    int num_workers = 1;
    int n = 10;

    // set workers to work
    vector<future<Counters>> workers(num_workers);
    for (auto& worker : workers) worker = async(computer, n);

    // collect results
    Counters result (n,0);
    for (auto& worker : workers)
    {
        Counters partial = worker.get();
        for (size_t i = 0; i != partial.size(); i++) result[i] += partial[i];
    }

    return 0;
}

我必须在这里做一些公然错误的事情。烦人的部分是,代码在 Win7 下的 MSVC2013 和 MinGW 上编译和运行良好,但在使用 g++4.8 的 Ubuntu 上产生这个神秘的崩溃:

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted (core dumped)

gdb 添加了这条重要的智慧:

Program received signal SIGABRT, Aborted.
0x00132416 in __kernel_vsyscall ()

#0  0x00132416 in __kernel_vsyscall ()
#1  0x00272e0f in raise () from /lib/i386-linux-gnu/libc.so.6
#2  0x00276455 in abort () from /lib/i386-linux-gnu/libc.so.6
#3  0x0017fc65 in __gnu_cxx::__verbose_terminate_handler() ()
   from /usr/lib/i386-linux-gnu/libstdc++.so.6
#4  0x0017d943 in ?? () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#5  0x0017d9bd in std::terminate() ()
   from /usr/lib/i386-linux-gnu/libstdc++.so.6
#6  0x0017dc81 in __cxa_throw () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#7  0x001d7ebd in std::__throw_system_error(int) ()
   from /usr/lib/i386-linux-gnu/libstdc++.so.6
#8  0x0804a1cd in _ZSt9call_onceIMNSt13__future_base11_State_baseEFvRSt8functionIFSt10unique_ptrINS0_12_Result_baseENS4_8_DeleterEEvEERbEJKPS1_St17reference_wrapperIS8_ESF_IbEEEvRSt9once_flagOT_DpOT0_ ()
#9  0x08049beb in std::__future_base::_State_base::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) ()
#10 0x0804f274 in std::__future_base::_Deferred_state<std::_Bind_simple<std::vector<unsigned int, std::allocator<unsigned int> > (*(int))(int)>, std::vector<unsigned int, std::allocator<unsigned int> > >::_M_run_deferred() ()
#11 0x08049afd in std::__future_base::_State_base::wait() ()
#12 0x0804b20d in std::__basic_future<std::vector<unsigned int, std::allocator<unsigned int> > >::_M_get_result() const ()
#13 0x0804a7c6 in std::future<std::vector<unsigned int, std::allocator<unsigned int> > >::get() ()
#14 0x0804960c in main ()

错误是由第 28 行的 worker.get(); 引起的。

我只在 VM 中使用 Ubuntu 来检查可移植性,我对本地调试环境不是很熟悉,所以我没有深入研究它。

问题是:这到底是怎么回事?

最佳答案

好的,答案是:使用 -pthread 编译它,否则...

关于c++ - g++4.8 处理 vector future 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27446425/

相关文章:

python - 为什么 asyncio.wait 不等待 FIRST_COMPLETED

c++ - 未评估的除以 0 是未定义的行为吗?

C++ 错误 : (class name) does not name a type

c++ - 由于模板函数声明中的歧义而出错

firebase - Flutter/Dart 和 Firebase 错误消息和代码

scala - 为什么编译器会错误地推断函数中 Future.sequence 声明的返回类型?

c++ - 为什么这个 const 说明符有未指定的行为?

c++ - 阅读复杂的 const 声明的简单规则?

c++ - 我什么时候以及为什么要使用-fno-elide-constructors?

c++ - 如何在 Cygwin 中禁用由 g++ 生成的扩展 .exe?