【问题标题】:Boost::Thread - thread create issueBoost::Thread - 线程创建问题
【发布时间】:2013-05-31 20:08:49
【问题描述】:

我的服务器有一个问题(基于 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 boost::system::error_code&)':

Core/CCore.cpp:71:163:错误:没有匹配的函数调用 'bind(, CCore* const, boost::arg (&)())'

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

CCore 定义和接受者:

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
}

有谁知道如何解决该错误? 谢谢。

【问题讨论】:

  • 发布sscce。我至少想看看class CCore的定义和CCore::handle_accept()的实现。
  • 已发布 CCore 定义和接受者。
  • 现在你的问题没有意义了,你删除了class CConnection 定义。请发送sscce,以便我们了解您的问题。我tried to make one 使用你的代码sn-ps,它编译得很好。
  • 嗯,对不起,我又添加了。

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


【解决方案1】:

由于handle_accept 是CCore 的成员函数,所以需要将boost::bind 的第一个参数改成:

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

到这里:

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

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 2015-12-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多