I am reading C++ concurrency in action by Anthony Williams.
I was trying to run a sample program implementing std::promise but its giving an error. Please let me know if anyone can help. Thanks.
Code :-
#include <iostream>
#include <future>
#include <thread>
using namespace std;
int myValue(int i,promise<int> intPromise)
{
cout<<"In myValue()"<<endl;
intPromise.set_value(i);
}
int main()
{
cout<<"In main()"<<endl;
promise<int> myPromise;
future<int> result=myPromise.get_future();
thread myThread(myValue,10,move(myPromise));
cout<<"Value : "<<result.get()<<endl;
}
I am not getting any error during compilation but while running this program i get following error.
terminate called without an active exception
Aborted (core dumped)
Though i am getting the output but i am also getting this error with it.
I am using g++ 4.7.0 on Fedora 17. Please help.
Looks like the main thread has finished before
myThread. In order to block main thread you should usestd::thread::join().Like this: