我对std :: promise的VC ++实现(Visual Studio 2015和2017)都有一个奇怪的问题。set_value_at_thread_exit()似乎不像广告中所宣传的那样工作-也许我误解了该标准所允许的和它所不允许的。以下代码将在clang中编译并正常运行,但是在使用VS2015(在第二个块中)或VS2017(在第三个块中)进行编译时会崩溃:
#include <future>
#include <thread>
#include <iostream>
int main()
{
{
std::cout << "Safe version... ";
std::promise<int> promise;
auto f = promise.get_future();
std::thread
(
[](std::promise<int> p)
{
p.set_value(99);
},
std::move(promise)
)
.detach();
std::cout << f.get() << std::endl;
}
{
std::cout << "Will crash VS2015... ";
std::promise<int> promise;
auto f = promise.get_future();
std::thread(
[p{ std::move(promise) }]() mutable
{
p.set_value_at_thread_exit(99);
}
)
.detach();
std::cout << f.get() << std::endl;
}
{
std::cout << "Will crash VS2017... ";
std::promise<int> …Run Code Online (Sandbox Code Playgroud)