C ++ std :: future在超时时挂起

时间:2018-07-13 00:12:18

标签: c++ visual-studio static-linking stdasync std-future

全部

我已经设置了一个测试项目来学习std :: future及其与std :: async的结合使用。这是一个简单的控制台应用程序,代码如下

#include <iostream>
#include <future>
#include <string>

using namespace std;

std::string getLine()
{
    char rawLine[1024];
    std::cin.getline(rawLine, 1024);
    std::string line(rawLine);
    return line;
}

int main(int argc, char *argv[]) 
{
    std::string line("");

    while ((line != "@shutdown"))
    {
        std::future<std::string> future = std::async(getLine);

        if (future.wait_for(std::chrono::seconds(30)) == std::future_status::ready)
            line = future.get();
        else
        {
            future._Abandon();
            std::cout << "@:-- server shutdown\n";
            break;
        }
    }

    return 0;
}

我注意到Visual Studion 2017的行为很奇怪。如果我将项目属性->代码生成->运行时库设置为/ MTd或/ MT,则该程序在退出时挂起,并且调用堆栈显示

>   futrreAsyncTest.exe!_execute_onexit_table::__l2::<lambda>() Line 206    C++ Symbols loaded.
    futrreAsyncTest.exe!__crt_seh_guarded_call<int>::operator()<void <lambda>(void),int <lambda>(void) &,void <lambda>(void) >(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, _execute_onexit_table::__l2::int <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 204 C++ Symbols loaded.
    futrreAsyncTest.exe!__acrt_lock_and_call<int <lambda>(void) >(const __acrt_lock_id lock_id, _execute_onexit_table::__l2::int <lambda>(void) && action) Line 940 C++ Symbols loaded.
    futrreAsyncTest.exe!_execute_onexit_table(_onexit_table_t * table) Line 160 C++ Symbols loaded.
    futrreAsyncTest.exe!common_exit::__l2::<lambda>() Line 226  C++ Symbols loaded.
    futrreAsyncTest.exe!__crt_seh_guarded_call<void>::operator()<void <lambda>(void),void <lambda>(void) &,void <lambda>(void) >(__acrt_lock_and_call::__l2::void <lambda>(void) && setup, common_exit::__l2::void <lambda>(void) & action, __acrt_lock_and_call::__l2::void <lambda>(void) && cleanup) Line 225    C++ Symbols loaded.
    futrreAsyncTest.exe!__acrt_lock_and_call<void <lambda>(void) >(const __acrt_lock_id lock_id, common_exit::__l2::void <lambda>(void) && action) Line 940 C++ Symbols loaded.
    futrreAsyncTest.exe!common_exit(const int return_code, const _crt_exit_cleanup_mode cleanup_mode, const _crt_exit_return_mode return_mode) Line 201 C++ Symbols loaded.
    futrreAsyncTest.exe!exit(int return_code) Line 293  C++ Symbols loaded.


    function    0x00d9f750 {futrreAsyncTest.exe!Concurrency::details::`anonymous namespace'::`dynamic atexit destructor for '_Task_scheduler_main_block_instance''(void)}   void(*)()

另一方面,如果我将项目属性->代码生成->运行时库设置为/ MD或/ MDd,则此程序可以正常退出。

有什么想法可能会发生这种情况吗?感谢任何帮助,谢谢!

0 个答案:

没有答案