<bdo id='CXRiM'></bdo><ul id='CXRiM'></ul>
  1. <legend id='CXRiM'><style id='CXRiM'><dir id='CXRiM'><q id='CXRiM'></q></dir></style></legend>

      <i id='CXRiM'><tr id='CXRiM'><dt id='CXRiM'><q id='CXRiM'><span id='CXRiM'><b id='CXRiM'><form id='CXRiM'><ins id='CXRiM'></ins><ul id='CXRiM'></ul><sub id='CXRiM'></sub></form><legend id='CXRiM'></legend><bdo id='CXRiM'><pre id='CXRiM'><center id='CXRiM'></center></pre></bdo></b><th id='CXRiM'></th></span></q></dt></tr></i><div id='CXRiM'><tfoot id='CXRiM'></tfoot><dl id='CXRiM'><fieldset id='CXRiM'></fieldset></dl></div>

      <small id='CXRiM'></small><noframes id='CXRiM'>

      <tfoot id='CXRiM'></tfoot>

      来自 std::promise 的未知异常

      时间:2023-08-25
      • <i id='cwGgX'><tr id='cwGgX'><dt id='cwGgX'><q id='cwGgX'><span id='cwGgX'><b id='cwGgX'><form id='cwGgX'><ins id='cwGgX'></ins><ul id='cwGgX'></ul><sub id='cwGgX'></sub></form><legend id='cwGgX'></legend><bdo id='cwGgX'><pre id='cwGgX'><center id='cwGgX'></center></pre></bdo></b><th id='cwGgX'></th></span></q></dt></tr></i><div id='cwGgX'><tfoot id='cwGgX'></tfoot><dl id='cwGgX'><fieldset id='cwGgX'></fieldset></dl></div>

        <legend id='cwGgX'><style id='cwGgX'><dir id='cwGgX'><q id='cwGgX'></q></dir></style></legend>

      • <tfoot id='cwGgX'></tfoot>
          <bdo id='cwGgX'></bdo><ul id='cwGgX'></ul>
            <tbody id='cwGgX'></tbody>
          1. <small id='cwGgX'></small><noframes id='cwGgX'>

                本文介绍了来自 std::promise 的未知异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                下面的代码有什么问题?运行时程序因未知异常中止

                What is wrong with the following code? When ran the program aborts with an unknown exception

                #include <iostream>
                #include <future>
                
                int main() {
                    auto promise = std::promise<int>{};
                    auto future_one = promise.get_future();
                    promise.set_value(1);
                
                    return 0;
                }
                

                错误输出为

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

                g++ --version 给我

                g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
                Copyright (C) 2015 Free Software Foundation, Inc.
                This is free software; see the source for copying conditions.  There is NO
                warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
                

                同样的代码在 mac 上运行良好

                The same code works just fine on a mac

                注意产生异常的代码行是promise.set_value(1)

                推荐答案

                简而言之,添加 -pthread 可以解决您的问题.

                In short, adding -pthread resolves your issue.

                $ g++ -std=c++14 -g -pthread -o temp temp.cpp
                $ ./temp
                


                详情

                我可以使用以下编译命令在 Ubuntu 16.04 上重现该行为:


                Details

                I can reproduce the behavior on Ubuntu 16.04 with below command on compiling:

                $ g++ -std=c++14 -g -o temp temp.cpp
                $ ./temp
                terminate called after throwing an instance of 'std::system_error'
                  what():  Unknown error -1
                Aborted (core dumped)
                

                GDB 转储显示:

                (gdb) bt
                #0  0x00007ffff74ab428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
                #1  0x00007ffff74ad02a in __GI_abort () at abort.c:89
                #2  0x00007ffff7ae484d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
                #3  0x00007ffff7ae26b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
                #4  0x00007ffff7ae2701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
                #5  0x00007ffff7ae2919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
                #6  0x00007ffff7b0b7fe in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
                #7  0x000000000040259b in 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*&&) (__once=...,
                    __f=<unknown type in /home/mine/tempdir/temp, CU 0x0, DIE 0xe578>) at /usr/include/c++/5/mutex:746
                #8  0x0000000000401e06 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) (this=0x61ac30, __res=..., __ignore_failure=false) at /usr/include/c++/5/future:387
                #9  0x0000000000402aee in std::promise<int>::set_value(int&&) (this=0x7fffffffe1c0, __r=<unknown type in /home/mine/tempdir/temp, CU 0x0, DIE 0xeb8a>) at /usr/include/c++/5/future:1075
                #10 0x0000000000401759 in main () at temp.cpp:7
                

                从转储中,我们可以看到它正在使用互斥锁等.然后我意识到std::future的东西依赖于线程,所以它需要链接到pthread,否则我们会看到这个异常.

                From the dump, we can see it's using mutex, etc. Then I realize that std::future stuff depends on thread, so it needs to link against pthread, otherwise we see this exception.

                std::thread

                这篇关于来自 std::promise 的未知异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:pthread_once() 中的竞争条件? 下一篇:为什么 Promise 库使用事件循环?

                相关文章

                <i id='yv0mx'><tr id='yv0mx'><dt id='yv0mx'><q id='yv0mx'><span id='yv0mx'><b id='yv0mx'><form id='yv0mx'><ins id='yv0mx'></ins><ul id='yv0mx'></ul><sub id='yv0mx'></sub></form><legend id='yv0mx'></legend><bdo id='yv0mx'><pre id='yv0mx'><center id='yv0mx'></center></pre></bdo></b><th id='yv0mx'></th></span></q></dt></tr></i><div id='yv0mx'><tfoot id='yv0mx'></tfoot><dl id='yv0mx'><fieldset id='yv0mx'></fieldset></dl></div>

                    <bdo id='yv0mx'></bdo><ul id='yv0mx'></ul>

                  1. <small id='yv0mx'></small><noframes id='yv0mx'>

                    <legend id='yv0mx'><style id='yv0mx'><dir id='yv0mx'><q id='yv0mx'></q></dir></style></legend>

                    <tfoot id='yv0mx'></tfoot>