ThreadPool class: 无法直接传递参数

ThreadPool class: can't pass the args straight

我有一个可以找到类型的线程池 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 添加 header 以确保完整性:

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

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