Boost :: Thread - 线程创建问题

时间:2013-05-31 20:08:50

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

我的服务器有一个问题(基于ASIO和Boost :: Thread)

排队:

this->connection->thread = boost::shared_ptr<boost::thread>(new boost::thread(worker, this->connection));

我收到了错误:

  

Core / CCore.cpp:在成员函数'void CCore :: handle_accept(const   升压::系统:: ERROR_CODE&安培;)':

     

Core / CCore.cpp:71:163:错误:没有匹配的调用函数   'bind(,CCore * const,   升压:: ARG&LT 1为卤素; (安培)())'

完整错误代码:http://pastebin.com/PaftWzbx

CCore定义&amp;接受者:

class CCore
{
public:
       CCore(boost::asio::io_service *io_service, const boost::asio::ip::tcp::endpoint &endpoint);
       bool failed;
private:
        void handle_accept( const boost::system::error_code& error );
        boost::asio::io_service *io_service;
        boost::asio::ip::tcp::endpoint endpoint;
        boost::asio::ip::tcp::acceptor *acceptor;
        boost::shared_ptr<CConnection> connection;
};

void CCore::handle_accept(const boost::system::error_code& error) 
{
    if (error) {
        // accept failed
        //LOG(ERROR, "Acceptor failed: " << error.message());
        return;
    }

    //LOG(INFO, "Accepted connection from " << this->connection->endpoint.address().to_string() << ":" << this->connection->endpoint.port());

    this->connection->thread = boost::shared_ptr<boost::thread>(new boost::thread(worker, this->connection));

    this->connection = boost::shared_ptr<CConnection>(new CConnection());
    this->connection->master_io_service = this->io_service;
    this->acceptor->async_accept(*(this->connection->socket), this->connection->endpoint, boost::bind(CCore::handle_accept, this, boost::asio::placeholders::error));
}
class CConnection {
  public:
    CConnection(void);
    boost::asio::io_service io_service;
    boost::shared_ptr<boost::asio::ip::tcp::socket> socket;
    boost::asio::ip::tcp::endpoint endpoint;
    boost::shared_ptr<boost::thread> thread;
    boost::asio::io_service *master_io_service;
    bool close;
};
void worker(boost::shared_ptr<CConnection> connection) 
{
    boost::asio::ip::tcp::socket &socket = *(connection->socket);
    boost::asio::socket_base::non_blocking_io make_non_blocking(true);
    socket.io_control(make_non_blocking);

    char acBuffer[1024];
    std::string line("");

    while ( connection->close == false ) {

    } // while connection not to be closed
}

有谁知道如何修复该错误的解决方案? 感谢。

1 个答案:

答案 0 :(得分:1)

由于handle_accept是CCore的成员函数,因此需要更改boost :: bind first参数:

boost::bind(CCore::handle_accept, ...

到此:

boost::bind(&CCore::handle_accept, ...

希望这有帮助