1

我只想拥有一个从套接字异步侦听并从boost::asio::posix::stream_descriptor分配给标准输入的聊天客户端。

如果我在单线程应用程序中运行此代码,一切正常。

如果我io_service.run()从 2 个或更多线程调用,来自标准输入的异步操作永远不会正常,但仍然执行来自套接字的异步读取。

这是代码:

MasterClient::MasterClient(boost::asio::io_service& io_service,boost::asio::ip::tcp::resolver::iterator iter, string nickName)
:it(iter),chatNick(nickName)
{
    this->fdIn_ = str_ptr(new boost::asio::posix::stream_descriptor(io_service,::dup(STDIN_FILENO)));
    this->dirServer_ = new(connectedPeer);
    this->dirServer_->sock = socket_ptr(new boost::asio::ip::tcp::socket(this->io_service_));
    boost::asio::async_connect(*(this->dirServer_->sock), this->it,
        boost::bind(&MasterClient::connectionHandler, this,
                boost::asio::placeholders::error));
}

主要的:

int main(int argc, const char * argv[])
{
boost::asio::io_service io_service(2);
    boost::asio::io_service::work work(io_service);
    boost::asio::ip::tcp::resolver resolver(io_service);
    boost::asio::ip::tcp::resolver::query query(argv[1], argv[2]);
    boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
    string nick;
    cout << "Inserire un nickname per la sessione di chat: " << flush;
    getline(cin,nick);
    MasterClient cli(io_service,iterator,nick);
cli.run();
}

和 MasterClient::run()

void MasterClient::run()
{
// Create a pool of threads to run all of the io_services.
std::vector<boost::shared_ptr<boost::thread> > threads;
boost::asio::io_service::work work(this->io_service_);
for (std::size_t i = 0; i < 1; ++i)
{
    boost::shared_ptr<boost::thread> thread(new boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_)));
    threads.push_back(thread);
}


// Wait for all threads in the pool to exit.
for (std::size_t i = 0; i < threads.size(); ++i)
    threads[i]->join();
}

这里我称之为异步读数:

void MasterClient::clientDelegate()
{
if(this->connectToServer())
{
    this->mainMenu();

    boost::asio::async_read_until(*fdIn_, inBuff_, '\n',
            boost::bind(&MasterClient::fdInMenuHandler,
                    this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));

    (*(this->dirServer_->sock)).async_read_some(boost::asio::buffer(this->buff_),
            boost::bind(&MasterClient::serverHandler,
                    this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred,
                    this->dirServer_->sock));
    this->io_service_.post(boost::bind(&MasterClient::printer,this));
}else
{
    if(this->isDebugging)
        cout << "Error in ClientDelegate." << endl;
}
if(this->isDebugging)
    cout << "ClientDelegate END" << endl;
}

连接处理程序,其中调用了 clientDelegate:

void MasterClient::connectionHandler(const boost::system::error_code& error)
{
cout << "connected" << endl;
try
{
    if(error)
        throw boost::system::system_error(error);
    else
    {
        this->dirServer_->endpoint = boost::asio::ip::tcp::endpoint((*(this->dirServer_->sock)).remote_endpoint());
        this->clientDelegate();
    }
}catch(const boost::system::system_error& e)
{
    cerr << "Boost Exception in ConnectHandler ---> " << e.what() << endl;
    this->io_service_.stop();
}
}

我究竟做错了什么?

4

1 回答 1

1

根据boost::asio::posix::stream_descriptor的文档,在多个线程中使用同一个实例是不安全的。在多线程情况下,通常将处理程序包装到一个链中以进行序列化。

在您的情况下,我看不到为同一个客户端连接使用多个线程的意义。

于 2013-01-23T10:44:38.167 回答