基本逻辑如下
主线程:
for(;;) {
accept socket
async_read head(length of packet),binded to head_handler
io_service.run() }
head_handler(非线程):
async_read body(using length read from head) ,binded to body_handler
body_handler(非线程):
parse message into Request objects (custom class).
push Request to request_pool
读线程:
for(;;) {
if request_pool not empty (lock otherwise) { perform actions described by the Request } }
所以我有一个使用上述逻辑的简单回显服务器。它会打印出传入的任何消息。但是,只会显示第一条消息,所有其他消息都会正确打印出“xxx.xxx.xxx.xxx connected”消息。但不会打印任何消息
最佳答案
如果你不发出任何异步。 body_handler
中的操作,io_service
运行结束,io_service::run()
退出。在对 io_service::run()
的任何后续调用之前,您必须调用 io_service::reset()
.
关于c++ - 带有 async_read 的 boost.asio 多线程 I/O 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914186/