c++ - 线程池编译错误

标签 c++ boost-asio threadpool boost-thread

当我尝试用一​​个任务编译我的线程池时,出现以下错误:

error: 'void ThreadPool::enqueue(F) [with F = CConnection::handle()::]', declared using local type 'CConnection::handle()::', is used but never defined [-fpermissive]

这是线程池声明:

class ThreadPool {
public:
    ThreadPool(size_t);
    template<class F>
    void enqueue(F f);
    ~ThreadPool();
private:
    // need to keep track of threads so we can join them
    std::vector< std::unique_ptr<boost::thread> > workers;

    // the io_service we are wrapping
    boost::asio::io_service service;
    boost::asio::io_service::work working;
    friend class Worker;
};

这里是函数,想用线程池测试一下:

void CConnection::handle()
{
     ThreadPool pool(4);
     pool.enqueue([1]
    {
        std::cout << "hello " << 1 << std::endl;
        boost::this_thread::sleep(
            boost::posix_time::milliseconds(1000)
        );
        std::cout << "world " << 1 << std::endl;
    });
     char * databuffer;
     databuffer = new char[16];
     for(int i = 0;i<16;i++)
     {
      databuffer[i] = 0x00;
     }
     databuffer[0] = 16;
     databuffer[4] = 1;
     databuffer[8] = 1;
     databuffer[12] = 1;
     asynchronousSend(databuffer, 16);

}

这里是入队定义:

template<class F>
void ThreadPool::enqueue(F f)
{
    service.post(f);
}

有人能发现我做错了什么吗?

最佳答案

是在ThreadPool.h头文件中定义了enqueue吗?这是模板方法所必需的

关于c++ - 线程池编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16877034/

相关文章:

c++ - 构造函数导致 "use of deleted function"错误

C++ Boost::ASIO 线程池问题

java - 将我的 C++ Base64 代码翻译成 Java 不工作

c++ - 如何在类声明之外定义嵌套在模板类中的模板类方法的特化?

c++ - 运行路径依赖库在 linux 上构建的程序的链接阶段无法找到其依赖项

c++ - 是否有等效于 Python 的 'in' 关键字的 c++?

c++ - 如何防止截止时间计时器调用已删除类中的函数?

c++ - 了解带有单个可调用的 boost::asio::post - 谁执行?

java - "Routing"threadpoolexecutor 内的任务

java - 池大小实际上如何与 Spring 的计划任务一起工作?