简体   繁体   English

为什么 std::promise-std::future 的单线程示例会抛出?

[英]Why does a single-threaded example of std::promise-std::future throw?

The following code throws std::system_error on recent g++, clang++ compilers and I do not know why.以下代码在最近的 g++、clang++ 编译器上抛出std::system_error ,我不知道为什么。 MSVC seems to work. MSVC 似乎工作。

I was under the impression multi-threaded context is not necessary for promise and future to work.我的印象是promisefuture工作不需要多线程上下文。

#include <future>
 
int main() {
    std::promise<int> p;
    std::future<int> f = p.get_future();
 
    p.set_value(1); //throws std::system_error
    return f.get();
}

Godbolt神螺栓

Stacktrace on my machine:我机器上的堆栈跟踪:

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff7c38537 in __GI_abort () at abort.c:79
#2  0x00007ffff7e8c7ec in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7e97966 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7e979d1 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff7e97c65 in __cxa_throw () from /lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7e8f458 in std::__throw_system_error(int) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x0000555555556fce in void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) ()
#8  0x00005555555569ae in std::__future_base::_State_baseV2::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) ()
#9  0x0000555555557417 in std::promise<int>::set_value(int&&) ()
#10 0x0000555555556346 in main ()

I fail to see how I am using std::promise::set_value incorrectly - p is valid and not set yet.我看不到我如何错误地使用std::promise::set_value - p有效且尚未设置。 Can anyone enlighten me please?任何人都可以启发我吗?

It works with clang++ -stdlib=libc++ so the issue is somewhere in libstdc++ , am I running into undefined behaviour or a bug?它与clang++ -stdlib=libc++一起使用,所以问题出在libstdc++的某个地方,我遇到了未定义的行为还是错误?

If I remember correctly, std::future depends on thread.如果我没记错的话, std::future取决于线程。 So, you would need to link against -pthread .因此,您需要链接到-pthread

Godbolt (with -pthread compiler option) https://godbolt.org/z/hPTMKqMjr Godbolt(带 -pthread 编译器选项) https://godbolt.org/z/hPTMKqMjr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM