【发布时间】:2011-08-31 01:06:01
【问题描述】:
我尝试与boost thread futures 合作。所以如图here我们可以从packaged task得到shared future。
所以我在linux上尝试了这样的功能:
template <class task_return_t>
void pool_item( boost::shared_ptr< boost::packaged_task<task_return_t> > pt)
{
boost::shared_future<task_return_t> fi= pt->get_future(); // error
//...
但我调用它时出错:
../../src/cf-util/thread_pool.h: In member function ‘void thread_pool::pool_item(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’:
../../src/cf-util/thread_pool.h:64:3: instantiated from ‘void thread_pool::post(boost::shared_ptr<boost::packaged_task<R> >) [with task_return_t = void]’
../../src/cf-server/server.cpp:39:27: instantiated from here
../../src/cf-util/thread_pool.h:124:58: error: conversion from ‘boost::unique_future<void>’ to non-scalar type ‘boost::shared_future<void>’ requested
我之前没有从该任务中获得任何未来。 all source code、place where I do call from、my thread pool that is being called。在 Visual Studio 2010 下的 Windows 上,它可以完美地编译和运行。
我该怎么办?如何修复或绕过此错误?
【问题讨论】:
-
可能是版本问题?我可以将
unique_future转换为shared_future就可以了。 -
@Luc Danton:告诉我怎么做,我会试试!)第二个问题是我将任务传递给我的任务管理器,然后将它们发送到这个函数中。所以这里的问题是
unique_future可以已经被占用\分配并在.wait();上。 -
就像您尝试的那样:
boost::shared_future<int> f = task.get_future();其中task是boost::packaged_task<int> task;。 (您的第二个问题听起来需要一个单独的问题。)
标签: c++ boost scheduled-tasks boost-thread future