BOOST ASIO多io_service RPC框架设计RFC [英] BOOST ASIO multi-io_service RPC framework design RFC

查看:674
本文介绍了BOOST ASIO多io_service RPC框架设计RFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个RPC框架,我想使用一个多io_service设计来解耦从执行IO(前端)的线程的 io_objects 执行RPC工作(后端)。



前端应该是单线程的,后端应该有一个线程池。我正在考虑一个设计,使前端和后端使用条件变量进行同步。但是,看来 boost :: thread boost :: asio 不要组合--ie,似乎条件变量 async_wait 支持不可用。我有一个关于此事的问题



此外,我假设你的头端是单线程的,只是为了避免从多个线程写入同一套接字的竞争条件。



使用 io_service :: strand 教程)。您的前端可以通过 io_service :: strand 。从后端到前端的所有帖子(以及从前端到前端的处理程序,如 handle_connect 等) 。)应该被 strand 包装,类似这样:



后端 - >前端: / p>

  io_service.post(front_end.strand.wrap(
boost :: bind(& Front_end :: send_response,front_end_ptr )));

或前端 - >前端:

  socket.async_connect(endpoint,strand.wrap(
boost :: bind(& Front_end :: handle_connect,shared_from_this(),
boost :: asio :: placeholder :: error)));

从前端到后端的所有帖子不应被 strand


I am working on a RPC framework, I want to use a multi io_service design to decouple the io_objects that perform the IO (front-end) from the the threads that perform the RPC work (the back-end).

The front-end should be single threaded and the back-end should have a thread pool. I was considering a design to get the front-end and back-end to synchronise using a condition variables. However, it seems boost::thread and boost::asio do not comingle --i.e., it seems condition variable async_wait support is not available. I have a question open on this matter here.

It occured to me that io_service::post() might be used to synchronise the two io_service objects. I have attached a diagram below, I just want to know if I understand the post mechanism correctly, and weather this is a sensible implementation.

解决方案

I assume that you use "a single io_service and a thread pool calling io_service::run()"

Also I assume that your frond-end is single-threaded just to avoid a race condition writing from multiple threads to the same socket.

The same goal can be achieved using io_service::strand (tutorial).Your front-end can be MT synchronized by io_service::strand. All posts from back-end to front-end (and handlers from front-end to front-end like handle_connect etc.) should be wrapped by strand, something like this:

back-end -> front-end:

io_service.post(front_end.strand.wrap(
    boost::bind(&Front_end::send_response, front_end_ptr)));

or front-end -> front-end:

socket.async_connect(endpoint, strand.wrap(
    boost::bind(&Front_end::handle_connect, shared_from_this(), 
    boost::asio::placeholders::error)));

And all posts from front-end to back-end shouldn't be wrapped by strand.

这篇关于BOOST ASIO多io_service RPC框架设计RFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆