I have a strange problem with VC ++ implementation (both Visual Studio 2015 and 2017) for std :: prom. set_value_at_thread_exit () does not seem to work as advertised, or maybe I misunderstand what the standard allows and what not. The following code will compile and work fine in clang, but will fail when compiling with VS2015 (in the second block) or VS2017 (in the third block):
#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> promise;
auto f = promise.get_future();
std::thread(
[](std::promise<int> p)
{
p.set_value_at_thread_exit(99);
},
std::move(promise)
)
.detach();
std::cout << f.get() << std::endl;
}
}
I tried to make the differences distinguishable with newlines.
, MS - " ", set_value_at_thead_exit(). , , . .
set_value_at_thread_exit() , f.get() future_error, .
MS (VS2017), , , , , . , API, , .
, VS2017 , VS2015 - , - std:: thread?!
,
clang: http://rextester.com/FEZDS24592
gcc: http://rextester.com/WFKE61563
VS2015: http://rextester.com/NODVFO14840
VS2017. : http://webcompiler.cloudapp.net/
, , , , ?