2017-05-19 40 views
2

在使用packaged_task时,我收集了向量中的所有期货。之后,我用get()推回未来的值。但是,我得到了错误的答案。谁能帮忙?非常感谢你。如何获得未来的价值?

#define BOOST_THREAD_PROVIDES_FUTURE 

#include <boost/thread/future.hpp> 
#include <vector> 
#include <iostream> 

using namespace std; 

vector<int> subFun(int n) { 

    vector<int> a{ 2 * n, 3 * n }; 

    return a; 
} 

int main() { 

    vector<boost::future<vector<int>>> g; 
    vector<vector<int>> x(10, vector<int>(2)); 
    int i; 

    for (i = 0; i < 10; i++) { 
     boost::packaged_task<vector<int>> task{ boost::bind(&subFun, i) }; 
     g.push_back(task.get_future()); 
     boost::thread t{ std::move(task) }; 
    } 

    for (auto& m : g) { 
     x.push_back(m.get()); 
    } 

    cout << x[3][0] << endl;//should be 6, now is 0 

    return 0; 
} 
+0

你为什么不使用'STD :: promise'?顺便说一句,这些现在都在标准库中。而且,标签C++ –

+0

想通了! –

回答

0

最真实的问题是,你的push_back成x,但你已经有了它初始化这里:

vector<vector<int>> x(10, vector<int>(2)); 

所以,你只需要添加10个元素,而不是把结果在指数0 ..9。我建议不预填充,如@帕特里克的答案,或者代替填充指定的时隙:

#define BOOST_THREAD_PROVIDES_FUTURE 

#include <boost/thread/future.hpp> 
#include <vector> 
#include <iostream> 

using namespace std; 

void subFun(int n, vector<int>& into) { 
    into = { 2 * n, 3 * n }; 
} 

int main() { 
    vector<boost::future<void>> futures; 
    vector<vector<int>> x(10, vector<int>(2)); 

    for (size_t i = 0; i < x.size(); i++) { 
     boost::packaged_task<void> task{ boost::bind(&subFun, i, std::ref(x[i])) }; 
     futures.push_back(task.get_future()); 
     boost::thread(std::move(task)).detach(); 
    } 

    for (auto& f : futures) 
     f.wait(); 

    cout << x[3][0] << endl; 
} 

你当然可以更复杂:

#define BOOST_THREAD_PROVIDES_FUTURE 

#include <boost/thread/future.hpp> 
#include <vector> 
#include <iostream> 

struct TaskResult { 
    int index; 
    std::vector<int> data; 
}; 

TaskResult subFun(int n) { 
    return { n, { 2 * n, 3 * n } }; 
} 

int main() { 
    std::vector<boost::future<TaskResult>> futures; 
    std::vector<std::vector<int>> x(10, std::vector<int>(2)); 

    for (size_t i = 0; i < x.size(); i++) { 
     boost::packaged_task<TaskResult> task{ boost::bind(&subFun, i) }; 
     futures.push_back(task.get_future()); 
     boost::thread(std::move(task)).detach(); 
    } 

    for (auto& f : futures) { 
     auto r = f.get(); 
     x[r.index] = r.data; 
    } 

    std::cout << x[3][0] << std::endl; 
} 
0

很多修修补补之后,我发现这个程序工作没有中止陷阱(我很惊讶你没有得到):

#include <future> 
#include <thread> 
#include <functional> 
#include <vector> 
#include <iostream> 

std::vector<int> subFun(int n) { 

    std::vector<int> a { 2 * n, 3 * n }; 

    return a; 
} 

int main() { 

    std::vector<std::future<std::vector<int>>> g; 
    std::vector<std::vector<int>> x; 

    int i; 

    for (i = 0; i < 10; i++) { 
     std::packaged_task<std::vector<int>(int)> task{ subFun }; 
     g.push_back(task.get_future()); 
     std::thread { std::move(task), i }.detach(); 
    } 

    for (auto& m : g) { 
     m.wait(); 
     x.push_back(m.get()); 
    } 

    std::cout << x[3][0] << std::endl; // is now 6 

    return 0; 
} 

转换为必要boostThis answer在寻找一些关键问题时非常有帮助。

+0

谢谢。我添加了m.wait(),但结果仍然是0而不是6. – Frankie

+0

中止陷阱可能是由于(无关的)比赛,其中'main'在线程消失之前结束。未连接的线程触发异常程序终止,除非分离。 – sehe

+0

非常感谢您的答复。我对未来有更清晰的了解。 – Frankie