<i id='Qh3Jk'><tr id='Qh3Jk'><dt id='Qh3Jk'><q id='Qh3Jk'><span id='Qh3Jk'><b id='Qh3Jk'><form id='Qh3Jk'><ins id='Qh3Jk'></ins><ul id='Qh3Jk'></ul><sub id='Qh3Jk'></sub></form><legend id='Qh3Jk'></legend><bdo id='Qh3Jk'><pre id='Qh3Jk'><center id='Qh3Jk'></center></pre></bdo></b><th id='Qh3Jk'></th></span></q></dt></tr></i><div id='Qh3Jk'><tfoot id='Qh3Jk'></tfoot><dl id='Qh3Jk'><fieldset id='Qh3Jk'></fieldset></dl></div>

      <small id='Qh3Jk'></small><noframes id='Qh3Jk'>

    1. <tfoot id='Qh3Jk'></tfoot>

    2. <legend id='Qh3Jk'><style id='Qh3Jk'><dir id='Qh3Jk'><q id='Qh3Jk'></q></dir></style></legend>
        <bdo id='Qh3Jk'></bdo><ul id='Qh3Jk'></ul>
      1. 我什么时候必须使用 boost::asio:strand

        时间:2023-07-19
            <tbody id='JWCoW'></tbody>
          • <tfoot id='JWCoW'></tfoot>

              <i id='JWCoW'><tr id='JWCoW'><dt id='JWCoW'><q id='JWCoW'><span id='JWCoW'><b id='JWCoW'><form id='JWCoW'><ins id='JWCoW'></ins><ul id='JWCoW'></ul><sub id='JWCoW'></sub></form><legend id='JWCoW'></legend><bdo id='JWCoW'><pre id='JWCoW'><center id='JWCoW'></center></pre></bdo></b><th id='JWCoW'></th></span></q></dt></tr></i><div id='JWCoW'><tfoot id='JWCoW'></tfoot><dl id='JWCoW'><fieldset id='JWCoW'></fieldset></dl></div>
            • <legend id='JWCoW'><style id='JWCoW'><dir id='JWCoW'><q id='JWCoW'></q></dir></style></legend>
            • <small id='JWCoW'></small><noframes id='JWCoW'>

                <bdo id='JWCoW'></bdo><ul id='JWCoW'></ul>

                  本文介绍了我什么时候必须使用 boost::asio:strand的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  看了boost::asio的文档,还是不清楚什么时候需要用到asio::strand.假设我有一个使用 io_service 的线程,那么在套接字上写入如下是否安全?

                  Reading the document of boost::asio, it is still not clear when I need to use asio::strand. Suppose that I have one thread using io_service is it then safe to write on a socket as follows ?

                  void Connection::write(boost::shared_ptr<string> msg)
                  {
                      _io_service.post(boost::bind(&Connection::_do_write,this,msg));
                  }
                  
                  void Connection::_do_write(boost::shared_ptr<string> msg)
                  {
                      if(_write_in_progress)
                      {
                        _msg_queue.push_back(msg);
                      }
                      else
                      {
                        _write_in_progress=true;
                        boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())),
                        boost::bind(&Connection::_handle_write,this,
                               boost::asio::placeholders::error));
                      }
                  }
                  
                  void Connection::_handle_write(boost::system::error_code const &error)
                  {
                    if(!error)
                    {
                      if(!_msg_queue.empty())
                      {
                            boost::shared_ptr<string> msg=_msg_queue.front();
                        _msg_queue.pop_front();
                        boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())),
                             boost::bind(&Connection::_handle_write,this,
                                     boost::asio::placeholders::error));
                          }
                      else
                      {
                        _write_in_progress=false;
                      }
                    }
                  }
                  

                  多线程调用 Connection::write(..) 或者我必须使用 asio::strand 吗?

                  Where multiple threads calls Connection::write(..) or do I have to use asio::strand ?

                  推荐答案

                  简短回答:不,在这种情况下您不必使用 strand.

                  Short answer: no, you don't have to use a strand in this case.

                  广泛简化,io_service 包含一个函数对象(处理程序)列表.当在服务上调用 post() 时,处理程序被放入列表中.例如每当异步操作完成时,处理程序及其参数都会放入列表中.io_service::run() 一个接一个地执行处理程序.因此,如果只有一个线程调用 run() 就像您的情况一样,则没有同步问题并且不需要 strand.
                  只有多个线程在同一个io_service上调用run(),多个handlers才会同时执行,N个线程最多N个并发handlers.如果这是一个问题,例如如果队列中可能有两个处理程序同时访问同一个对象,则需要 strand.
                  您可以将 strand 视为一组处理程序的一种锁.如果线程执行与 strand 关联的处理程序,该 strand 将被锁定,并在处理程序完成后释放.任何其他线程只能执行与锁定的 strand 无关的处理程序.

                  Broadly simplificated, an io_service contains a list of function objects (handlers). Handlers are put into the list when post() is called on the service. e.g. whenever an asynchronous operation completes, the handler and its arguments are put into the list. io_service::run() executes one handler after another. So if there is only one thread calling run() like in your case, there are no synchronisation problems and no strands are needed.
                  Only if multiple threads call run() on the same io_service, multiple handlers will be executed at the same time, in N threads up to N concurrent handlers. If that is a problem, e.g. if there might be two handlers in the queue at the same time that access the same object, you need the strand.
                  You can see the strand as a kind of lock for a group of handlers. If a thread executes a handler associated to a strand, that strand gets locked, and it gets released after the handler is done. Any other thread can execute only handlers that are not associated to a locked strand.

                  注意:这个解释可能过于简单化,技术上也不准确,但它给出了io_service中发生的事情的基本概念和 strands.

                  Caution: this explanation may be over-simplified and technically not accurate, but it gives a basic concept of what happens in the io_service and of the strands.

                  这篇关于我什么时候必须使用 boost::asio:strand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将 BOOST_FOREACH 与 boost::ptr_map 一起使用? 下一篇:是否可以使用 boost::filter_iterator 进行输出?

                  相关文章

                1. <tfoot id='RP1go'></tfoot>
                  <legend id='RP1go'><style id='RP1go'><dir id='RP1go'><q id='RP1go'></q></dir></style></legend>

                  • <bdo id='RP1go'></bdo><ul id='RP1go'></ul>

                2. <small id='RP1go'></small><noframes id='RP1go'>

                    <i id='RP1go'><tr id='RP1go'><dt id='RP1go'><q id='RP1go'><span id='RP1go'><b id='RP1go'><form id='RP1go'><ins id='RP1go'></ins><ul id='RP1go'></ul><sub id='RP1go'></sub></form><legend id='RP1go'></legend><bdo id='RP1go'><pre id='RP1go'><center id='RP1go'></center></pre></bdo></b><th id='RP1go'></th></span></q></dt></tr></i><div id='RP1go'><tfoot id='RP1go'></tfoot><dl id='RP1go'><fieldset id='RP1go'></fieldset></dl></div>