Question: What is the correct way to store a collection of promises?
I am using g ++ (Ubuntu 4.8.4-2ubuntu1 ~ 14.04) 4.8.4 with "-std = C ++ 11". (update: now upgraded to g ++ 5.3, see comments)
The following code throws an exception when expect.set_value () is called. The message "ends with a call after calling the instance" std :: system_error "what (): Unknown error -1 Aborted (kernel is reset)"
#include <vector>
#include <future>
int main()
{
{
std::vector<std::promise<int>> q;
std::promise<int> p;
auto f = p.get_future();
q.push_back(std::move(p));
auto t1 = std::move(q.front());
t1.set_value(10);
}
}
If I replace std :: prom with boost :: prom, it works
#include <vector>
#include <boost/thread/future.hpp>
int main()
{
{
std::vector<boost::promise<int>> q;
boost::promise<int> p;
auto f = p.get_future();
q.push_back(std::move(p));
auto t1 = std::move(q.front());
t1.set_value(10);
}
}
The most amazing thing is that std :: prom :: set_value () does not throw an exception (or maybe they will catch it?) If I just save the boost :: prom declaration in the program!
#include <vector>
#include <future>
#include <boost/thread/future.hpp>
int main()
{
{
boost::promise<int> p;
}
{
std::vector<std::promise<int>> q;
std::promise<int> p;
auto f = p.get_future();
q.push_back(std::move(p));
auto t1 = std::move(q.front());
t1.set_value(10);
}
}
:
-Wextra -Wall -Werror -std = ++ 11 [, (void) argc (void) argv ]
ldd output
linux-vdso.so.1 => (0x00007fffe51fe000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa542df1000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa542bda000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa542813000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa54250d000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa54311f000)