模板化std::future返回的问题
Issue with a templated std::future return
我使用c++ 11创建了一个线程池类,如下所示。
#include <iostream>
#include <functional>
#include <vector>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <queue>
#include <atomic>
#include <future>
class threadpool {
struct task {
std::size_t m_priority;
std::function<void()> m_fn;
bool operator()(task* t1, task* t2) const {
return t1->m_priority < t2->m_priority;
}
};
public:
threadpool(std::size_t nthreads);
~threadpool();
// void assign(std::function<void()> fn, std::size_t priority);
template <typename T> std::future<T> assign(std::function<T()> fn, std::size_t priority);
private:
void funnel();
std::vector<std::thread*> m_threadlist;
std::priority_queue<task*, std::vector<task*>, task> m_tasks;
std::mutex m_tasks_mtx;
std::condition_variable m_newc;
std::mutex m_newc_mtx;
std::atomic<bool> m_destruct;
};
threadpool::threadpool(std::size_t nthreads) {
for (std::size_t _n = nthreads; _n > 0; --_n)
m_threadlist.push_back(new std::thread(std::bind(&threadpool::funnel, this)));
}
threadpool::~threadpool() {
m_destruct = true;
while (!m_threadlist.empty()) {
m_newc.notify_one();
}
}
/* ... */
template <typename T>
std::future<T> assign(std::function<T()> fn, std::size_t priority) {
std::promise<T> _prom;
std::future<T> _ft = _prom.get_future();
threadpool::task* _t = new task();
_t->m_fn = std::bind([](std::promise<T>& p) {
p.set_value(fn());
}, std::ref(_prom));
_t->m_priority = priority;
{
std::lock_guard<std::mutex> _lock(m_tasks_mtx);
m_tasks.push(_t);
}
m_newc.notify_one();
return _ft;
}
void threadpool::funnel() {
while (!m_destruct) {
{
std::unique_lock<std::mutex> _lock(this->m_newc_mtx);
m_newc.wait(_lock);
}
for (;;) {
threadpool::task* _t;
{
std::lock_guard<std::mutex> _lock(m_tasks_mtx);
if (m_tasks.empty()) break;
_t = m_tasks.top();
m_tasks.pop();
}
_t->m_fn();
delete _t;
}
}
}
int count() {
return 3;
}
int main(int argc, char* argv[])
{
threadpool* herppool = new threadpool(10);
for(int i = 0; i<900;i++) {
auto derp = herppool->assign<int>(count,3); // <-- error here
//printf("%d", derp.get());
}
delete herppool;
//getchar();
}
我无法解决为什么在指定的行MSVC 2012下出现以下错误。
1> main.cpp
1>main.obj : error LNK2001: unresolved external symbol "public: class std::future<int> __thiscall threadpool::assign<int>(class std::function<int __cdecl(void)>,unsigned int)" (??$assign@H@threadpool@@QAE?AV?$future@H@std@@V?$function@$$A6AHXZ@2@I@Z)
1>C:Users...visual studio 2012ProjectsthreadpoolReleasethreadpool.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
对我来说,这个函数是存在的。我不知道链接器在抱怨什么,非常感谢一些帮助跟踪错误。我已经盯着它看了好几个小时了
如果你仔细看这里:
template <typename T>
std::future<T> assign(std::function<T()> fn, std::size_t priority)
您不是定义threadpool::assign
,而是定义一个名为assign
的自由函数。链接器很清楚地告诉你,你忘记定义threadpool::assign
了。你需要这样做:
template <typename T>
std::future<T> threadpool::assign(std::function<T()> fn, std::size_t priority)
相关文章:
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 从 C++ 中的函数返回数组地址问题
- 在类中返回结构时出现问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 这个返回元素位置的基于循环的函数有什么问题?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 特征返回类型的 pybind11 问题
- 类的大问题,以及从空函数c++返回
- 基本的 c++ 问题:如果我在函数中创建某些内容并返回它会发生什么?
- C++模板编程设计问题 - 根据输入文件返回不同的类型
- 存储另一个函数返回的布尔数组时遇到问题
- 为什么在分配给成员变量之前获取unique_ptr的返回是一个问题?
- 从 C++ 中的函数返回双精度的问题
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 字符 * 未从重载运算符或内存管理问题正确返回
- 带有指针返回问题的c++协方差问题
- 在 c++ 中使用 sqlite 的 void 函数中的"返回"问题
- C++动态矩阵乘法返回问题
- C++ 无限循环,返回问题
- STL 列表<mystruct>返回问题