从future中检索值时SIGABRT

SIGABRT when retrieving value from future

本文关键字:SIGABRT 检索 future      更新时间:2023-10-16

我有一个使用c++ 11期货的问题。当我在std::async返回的future上调用wait()get()时,程序接收到从mutex头抛出的SIGABRT信号。有什么问题吗?如何解决这个问题?

我在Linux上使用g++ 4.6。将以下代码粘贴到ideone.com会导致同样的问题。

#include <future>
#include <thread>
int calculate() {
    return 1;
}
int main() {
    auto result = std::async(calculate);
    result.wait();// <-- this aborts
    int value = result.get();// <-- or this aborts as well if previous line is commented out.
    return 0;
}

可以通过将-pthread开关添加到g++中来解决这个问题。