ThreadPool类:不能直接传递args

时间:2015-01-15 19:32:57

标签: c++ multithreading c++11

我有一个可以找到的类型的线程池here

我尝试执行以下操作:

/* Allocate a ThreadPool */
shared_ptr<SpinTools::ThreadPool> pool(new SpinTools::ThreadPool(4));

/* Start processing each image in Core container */
for (int img_idx = 0; img_idx < num_images; img_idx++) {

    pool->enqueue(_worker_task, img_idx, _args, _stats);

}

我的工作人员任务有以下原型:

void _worker_task(int img_idx, ProcessorArgs &args, Stats &stats)

但是这不会编译,MSVC会显示错误

Error   14  error LNK2019: unresolved external symbol "public: class std::future<void> __cdecl ThreadPool::enqueue<void (__cdecl&)(int,struct ProcessorArgs,struct SpinCore::Features::Stats &),int &,struct Sift::ProcessorArgs &,struct SpinCore::Features::Stats &>(void (__cdecl&)(int,struct ProcessorArgs,struct SpinCore::Features::Stats &),int &,struct ProcessorArgs &,struct SpinCore::Features::Stats &)" (??$enqueue@A6AXHUProcessorArgs@Sift@@AEAUStats@Features@SpinCore@@@ZAEAHAEAU12@AEAU345@@ThreadPool@SpinTools@@QEAA?AV?$future@X@std@@A6AXHUProcessorArgs@Sift@@AEAUStats@Features@SpinCore@@@ZAEAHAEAU45@1@Z) referenced in function "public: virtual void __cdecl Sift::process(void)" (?process@Sift@@UEAAXXZ) 

编辑:为完整性添加ThreadPool :: enqueue的标题:

template<class F, class... Args>
auto enqueue(F&& f, Args&&... args) 
    -> std::future<typename std::result_of<F(Args...)>::type>;

1 个答案:

答案 0 :(得分:1)

您需要在头文件中放置ThreadPool :: enqueue的实现。如果MSVC仅在.cpp文件中(或至少早期版本不能),则无法生成实现