copy and paste this and compile.
#include <thread>
#include <cassert>
#include <iostream>
#include <string>
#include <future>
#include <utility>
void ThFun(std::promise<std::string>& prms)
{
std::string hello = "Hello From Future!\n";
prms.set_value(hello);
}
int main()
{
std::promise<std::string> prms;
auto ftr = prms.get_future();
std::thread th(&ThFun, std::move(prms));
std::cout << "Hello from Main\n";
std::string str = ftr.get();
std::cout << str << std::endl;
th.join();
return 0;
}
1>------ Build started: Project: PromisesAndFutures, Configuration: Debug Win32 ------
1>Build started 4/20/2012 9:27:43 AM.
1>InitializeBuildStatus:
1> Creating "Debug\PromisesAndFutures.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> Main.cpp
1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\tuple(119): error C2248: 'std::promise<_Ty>::promise' : cannot access private member declared in class 'std::promise<_Ty>'
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\future(1575) : see declaration of 'std::promise<_Ty>::promise'
1> with
1> [
1> _Ty=std::string
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\tuple(486) : see reference to function template instantiation 'std::_Tuple_val<_Ty>::_Tuple_val<const _Ty&>(_Other)' being compiled
1> with
1> [
1> _Ty=std::promise<std::string>,
1> _Other=const std::promise<std::string> &
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\tuple(486) : while compiling class template member function 'std::tuple<<unnamed-symbol>>::tuple(const std::tuple<<unnamed-symbol>> &)'
1> with
1> [
1> <unnamed-symbol>=std::promise<std::string>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional(1140) : see reference to class template instantiation 'std::tuple<<unnamed-symbol>>' being compiled
1> with
1> [
1> <unnamed-symbol>=std::promise<std::string>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(50) : see reference to class template instantiation 'std::_Bind<_Forced,_Ret,_Fun,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,<unnamed-symbol>>' being compiled
1> with
1> [
1> _Forced=true,
1> _Ret=void,
1> _Fun=void (__cdecl *const )(std::promise<std::string> &),
1> _V0_t=std::promise<std::string>,
1> _V1_t=std::_Nil,
1> _V2_t=std::_Nil,
1> _V3_t=std::_Nil,
1> _V4_t=std::_Nil,
1> _V5_t=std::_Nil,
1> <unnamed-symbol>=std::_Nil
1> ]
1> c:\users\jim\documents\visual studio 11\projects\promisesandfutures\promisesandfutures\main.cpp(18) : see reference to function template instantiation 'std::thread::thread<void(__cdecl *)(std::promise<_Ty> &),std::promise<_Ty>>(_Fn,_V0_t &&)' being compiled
1> with
1> [
1> _Ty=std::string,
1> _Fn=void (__cdecl *)(std::promise<std::string> &),
1> _V0_t=std::promise<std::string>
1> ]
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.69
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1 succeeded, 0 failed, 0 up-to-date, 0 skipped