使用std::async遇到的异常问题
1.crash 在std中 thread 和 condition_variable相关的地方
2.导致线程卡住
vs2013下使用没问题,升级到vs2015后出现
#include "stdafx.h"
#include <stdarg.h>
#include <thread>
#include <future>
#include <iostream>
#include <windows.h>
using namespace std;
void connect()
{
std::async(std::launch::async, [](){
for (int i = 0; i < 5; ++i)
{
Sleep(1000);
cout << i << endl;
}
});
cout<< "finished connection" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
connect();
while (1){
cout << "main thread" << endl;
Sleep(500);
}
cout << "end connection" << endl;
return 0;
}
这样的写法在vs2013中没有问题,但在 vs2015中就引发以上两个问题,甚至间接的引发别的异常
在vs2015编译器下,包括在gcc编译器下,该异步方法会阻塞调用它的线程,直到子线程结束。而在vs2013的编译器下,异步方法不会阻塞当前线程.
std::future<T>::~future
C++
Thread support library
std::future
~future();
(since C++11)
Releases any shared state. This means
if the return object or provider holds the last reference to its shared state, the shared state is destroyed; and
the return object or provider give