Hi I don't know how to write a proper bind to queue and execute a lambda expression passed to the method 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;
}
Currently this fails to compile on "auto task =" because f expects a parameter of type OCRK* with:
Error 6 error C2064: term does not evaluate to a function taking 0 arguments 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) : see reference to function template instantiation 'std::shared_ptr> std::make_shared,std::_Bind &,>>(std::_Bind &,> &&)' being compiled
The expected usage is like this:
OCRQueue ocrPool(std::thread::hardware_concurrency());
auto work = [](OCRK* ocr)
{
return ocr->DoSomething();
};
future<DoSomethingResult> result = ocrPool.enqueue(work);
The OCRK* will be passed to the enqueued function when dequeued and executed in another thread.