简体   繁体   English

Boost async_read_some不完全异步

[英]Boost async_read_some not exactly asynchronous

This is my server code: 这是我的服务器代码:

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

If i write a the the following code in a loop 如果我在循环中编写以下代码

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

in the 'handle_read_header' function which is called by the above 'async_read_some' the whole thread is waiting till the sleep end. 在上面的“ async_read_some”调用的“ handle_read_header”函数中,整个线程都在等待直到睡眠结束。 So when another request comes in it is not handled until the sleep finishes. 因此,当另一个请求进入时,直到睡眠完成才处理该请求。 Isn't is suppose to asynchronously handles each requests? 是不是应该异步处理每个请求? I am new to boost and C++. 我是Boost和C ++的新手。 Please let me know if i have mentioned anything wrong. 如果我有提及任何错误,请告诉我。

Read scheduled with async_read_some is realized in the thread which called io_service::run() . 使用async_read_some调度的读取在名为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. 您可以通过使用io_service::run()运行更多线程来创建线程池,或者缩短读取处理程序的执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM