2013-02-14 47 views
10

在下面的示例代码,我想从一个Component创建Item对象:从中间期货创造未来?

struct Component { }; 

struct Item { 
    explicit Item(Component component) : comp(component) {}  
    Component comp; 
}; 

struct Factory { 
    static std::future<Item> get_item() { 
     std::future<Component> component = get_component();   
     // how to get a std::future<Item> ? 
    } 

    std::future<Component> get_component(); 
}; 

如何从std::future<Component>std::future<Item>


更新:删除我的第一个想法(这是线程为基础)从问题和张贴答案。

+0

属于上http://codereview.stackexchange.com? – 2013-02-14 14:30:32

+0

一个侧面说明 - 更好地在Item的构造函数中使用const ref。 – 2013-02-14 14:31:27

+0

@LightnessRacesinOrbit它如何评论问题? 。他正在寻找更好的方法/方式! – M3taSpl0it 2013-02-14 14:31:41

回答

3

它发生,我认为我可以使用std::async推迟启动策略组成的最终目标:

std::future<Item> get_item() 
{ 
    // start async creation of component 
    // (using shared_future to make it copyable) 
    std::shared_future<Component> component = get_component(); 

    // deferred launch policy can be used for construction of the final object 
    return std::async(std::launch::deferred, [=]() { 
     return Item(component.get()); 
    }); 
} 
+0

谢谢,这是我正在寻找的答案。我需要一种方法来有效地构建一个'std :: future'对象,但是线程的使用将是完全不必要的。 – 2014-01-17 19:14:27

13

需要moar packaged_tasks!

std::future<Item> get_item() { 
    std::packaged_task<Item()> task([]{ 
     return Item(get_component().get()); 
    }); 
    auto future = task.get_future(); 
    std::thread(std::move(task)).detach(); 
    return future; 
}; 

一般来说,我建议忘记承诺并首先考虑packaged_tasks。 A packaged_task负责为您维护一个(功能,承诺,未来)三联。它允许你以自然的方式编写函数(即使用返回和抛出等),并正确地将异常传播到将来,这是你的例子忽略的(在你的程序中,未处理的异常在任何线程std::terminate!)。

+0

+1对于mo re代表 – 2013-02-14 14:41:16

+0

没有更好的方法吗?我想知道为什么还有另一个线程需要,如果所有的用户正在尝试隐藏一些内部实现并暴露一个更好的接口。 – balki 2013-02-15 12:22:24

+0

@balki不符合标准的东西。有计划添加'future.then()',那就行了。 – 2013-02-15 12:34:27

2

您还可以使用Herb Sutter提出的then函数。这是一个稍微修改过的功能版本。有关它如何修改的更多信息以及与原始对话的链接可在this SO question中找到。您的代码将归结为:

return then(std::move(component), [](Component c) { return Item(c); }); 

最初的想法是让功能then作为std::future<T>一个成员函数并有一些正在进行的将其放入标准的工作。该函数的第二个版本用于void期货(基本上只是异步链接功能)。正如Herb指出的那样,您可能需要额外的线程来支付使用这种方法的费用。

您的代码应该是这样的:

#include <future> 
#include <thread> 
#include <iostream> 


template <typename T, typename Work> 
auto then(std::future<T> f, Work w) -> std::future<decltype(w(f.get()))> 
{ 
    return std::async([](std::future<T> f, Work w) 
        { return w(f.get()); }, std::move(f), std::move(w)); 
} 

template <typename Work> 
auto then(std::future<void> f, Work w) -> std::future<decltype(w())> 
{ 
    return std::async([](std::future<void> f, Work w) -> decltype(w()) 
        { f.wait(); return w(); }, std::move(f), std::move(w)); 
} 

struct Component { }; 

struct Item { 
    Item(Component component) : comp(component) {} 
    Component comp; 
}; 


struct Factory { 
    static std::future<Item> get_item() { 
    std::future<Component> component = get_component(); 
    return then(std::move(component), [](Component c) { return Item(c); }); 
    } 

    static std::future<Component> get_component() 
    { 
    return std::async([](){ return Component(); }); 
    } 

}; 

int main(int argc, char** argv) 
{ 
    auto f = Factory::get_item(); 
    return 0; 
} 

上面的代码编译罚款铿锵或是libC++(在Mac OS X 10.8测试)。