0

これは私のサーバーコードです:

socket_.async_read_some(boost::asio::buffer(data_read.data(), Message::header_length),
    boost::bind(&TcpConnection::handle_read_header, shared_from_this(),
    boost::asio::placeholders::error));

次のコードをループで記述した場合

boost::thread::sleep(boost::posix_time::seconds(2));

上記の「async_read_some」によって呼び出される「handle_read_header」関数では、スレッド全体がスリープが終了するまで待機しています。したがって、別のリクエストが着信すると、スリープが終了するまで処理されません。各リクエストを非同期的に処理することを想定していませんか?私はブーストとC++に不慣れです。何か間違ったことを言ったら教えてください。

4

1 に答える 1

4

Read scheduled with async_read_some is realized in the thread which called io_service::run(). If you have only one thread it will wait for completing one read handler, before starting another one.

You can make a thread pool, by running more threads with io_service::run() or make the execution of read handler shorter.

于 2012-06-18T06:58:16.013 に答える