创建接受转发参数的共享 packaged_task
Create shared packaged_task that accepts a parameter with forwarding
嗨,我不知道如何编写一个正确的队列绑定并执行传递给方法的 lambda 表达式 OCRQueue::enqueue()
// the task queue
std::queue< std::function<void(OCRK*)> > tasks;
// add new work item to the pool
template<class F>
auto OCRQueue::enqueue(F&& f)
-> std::future<typename std::result_of<F(OCRK*)>::type>
{
using return_type = typename std::result_of<F(OCRK*)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >
(
// how to initialize this task so that it can be called
// task(OCRK*) passing the parameter to f(OCRK*)
std::bind
(
std::forward<F>(f)
)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if (stop)
throw std::runtime_error("enqueue on stopped thread_pool");
// this fails because task does not accept parameters
tasks.emplace([task](OCRK* ocr){ task(ocr); });
}
condition.notify_one();
return res;
}
目前无法在 "auto task =" 上编译,因为 f 需要一个 OCRK* 类型的参数:
错误 6 error C2064: term 不求值为带 0 个参数的函数 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap 58 1 Skilja.PR.API
On: 1> d:\sourcecode\skilja\alpr\alpr\skilja.pr.api.native\OCRQueue.h(51) : 请参阅函数模板实例化参考 'std::shared_ptr> std::make_shared,std::_Bind &,>>(std::_Bind &,> &&)' 正在编译
预期的用法是这样的:
OCRQueue ocrPool(std::thread::hardware_concurrency());
auto work = [](OCRK* ocr)
{
return ocr->DoSomething();
};
future<DoSomethingResult> result = ocrPool.enqueue(work);
OCRK* 将在出队并在另一个线程中执行时传递给入队函数。
要创建接受单个参数的可调用对象,您需要告诉 bind
为参数使用占位符:
std::bind( std::forward<F>(f), std::placeholders::_1 )
这告诉 bind
留下一个未绑定的参数,当您调用函数对象时必须提供该参数。
但我根本不明白你为什么要使用 bind
,你没有绑定任何东西并且 f
已经是一个接受单个参数的可调用对象,为什么不您只是将其传递给 packaged_task
构造函数?
还有一个问题,就是你说你想要一个接受单个参数的任务,但是你声明了 packaged_task<return_type()>
这是一个不带参数的任务。我想你想要:
auto task = std::make_shared< std::packaged_task<return_type(OCRK*)> >(std::forward<F>(f));
还有:
tasks.emplace([task](OCRK* ocr){ task(ocr); });
仍然会失败,因为 task
是一个 shared_ptr
,所以它不可调用。应该是:
tasks.emplace([task](OCRK* ocr){ (*task)(ocr); });
嗨,我不知道如何编写一个正确的队列绑定并执行传递给方法的 lambda 表达式 OCRQueue::enqueue()
// the task queue
std::queue< std::function<void(OCRK*)> > tasks;
// add new work item to the pool
template<class F>
auto OCRQueue::enqueue(F&& f)
-> std::future<typename std::result_of<F(OCRK*)>::type>
{
using return_type = typename std::result_of<F(OCRK*)>::type;
auto task = std::make_shared< std::packaged_task<return_type()> >
(
// how to initialize this task so that it can be called
// task(OCRK*) passing the parameter to f(OCRK*)
std::bind
(
std::forward<F>(f)
)
);
std::future<return_type> res = task->get_future();
{
std::unique_lock<std::mutex> lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if (stop)
throw std::runtime_error("enqueue on stopped thread_pool");
// this fails because task does not accept parameters
tasks.emplace([task](OCRK* ocr){ task(ocr); });
}
condition.notify_one();
return res;
}
目前无法在 "auto task =" 上编译,因为 f 需要一个 OCRK* 类型的参数:
错误 6 error C2064: term 不求值为带 0 个参数的函数 C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xrefwrap 58 1 Skilja.PR.API
On: 1> d:\sourcecode\skilja\alpr\alpr\skilja.pr.api.native\OCRQueue.h(51) : 请参阅函数模板实例化参考 'std::shared_ptr> std::make_shared,std::_Bind &,>>(std::_Bind &,> &&)' 正在编译
预期的用法是这样的:
OCRQueue ocrPool(std::thread::hardware_concurrency());
auto work = [](OCRK* ocr)
{
return ocr->DoSomething();
};
future<DoSomethingResult> result = ocrPool.enqueue(work);
OCRK* 将在出队并在另一个线程中执行时传递给入队函数。
要创建接受单个参数的可调用对象,您需要告诉 bind
为参数使用占位符:
std::bind( std::forward<F>(f), std::placeholders::_1 )
这告诉 bind
留下一个未绑定的参数,当您调用函数对象时必须提供该参数。
但我根本不明白你为什么要使用 bind
,你没有绑定任何东西并且 f
已经是一个接受单个参数的可调用对象,为什么不您只是将其传递给 packaged_task
构造函数?
还有一个问题,就是你说你想要一个接受单个参数的任务,但是你声明了 packaged_task<return_type()>
这是一个不带参数的任务。我想你想要:
auto task = std::make_shared< std::packaged_task<return_type(OCRK*)> >(std::forward<F>(f));
还有:
tasks.emplace([task](OCRK* ocr){ task(ocr); });
仍然会失败,因为 task
是一个 shared_ptr
,所以它不可调用。应该是:
tasks.emplace([task](OCRK* ocr){ (*task)(ocr); });