I am learning the std::promise
and std::future
in C++. I wrote one simple program to calculate the multiplication of two numbers.
void product(std::promise<int> intPromise, int a, int b)
{
intPromise.set_value(a * b);
}
int main()
{
int a = 20;
int b = 10;
std::promise<int> prodPromise;
std::future<int> prodResult = prodPromise.get_future();
// std::thread t{product, std::move(prodPromise), a, b};
product(std::move(prodPromise), a, b);
std::cout << "20*10= " << prodResult.get() << std::endl;
// t.join();
}
In the above code if I invoke the product
function using threads it's working fine. But if I invoke the function using direct function call I am getting the following error:
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
I added some logs to check the problem. I am getting the error while setting the value (set_value
) in the function product
. Is there anything I missed in the code?