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


  错误:'无效ThreadPool :: enqueue(F)[与F =
  CConnection :: handle()::]',使用本地类型声明
  'CConnection :: handle()::',已使用但从未定义
  [-放任]


这是线程池声明:

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);
}


有人可以发现我做错了吗?

最佳答案

enqueue的定义是否在ThreadPool.h标头中?这是模板方法所必需的

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

10-09 03:00