C ++多线程服务器的帮助 [英] C++ Multithreaded server help

查看:99
本文介绍了C ++多线程服务器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C工作一个多线程的服务器上++用升压ASIO。目前设计问题我遇到涉及擦除的连接。

I'm working on a multithreaded server in c++ using boost-asio. Currently a design problem I'm running into deals with erasing a connection.

我持有的连接对象的矢量单个服务器实例。这些连接接收我分析的命令。在将数据发送到我的向量中的所有连接特定的交易之一命令。

I have a single server instance which holds a vector of connection objects. These connections receive commands which I parse. One command in particular deals with sending data to ALL connections in my vector.

现在,当连接断开时我目前擦除从矢​​量该连接并调用析构函数。好像我要遇到问题时,有人SendAll在同一时间有人断开连接。

Now when a connection disconnects I'm currently erasing this connection from the vector and calling the destructor. It seems like I'm going to run into problems when someone 'SendAll' at the same time someone 'Disconnect'.

谁能推荐一个更好的设计,或只是点我朝着正确的方向?任何帮助非常AP preciated。谢谢

Could anyone recommend a better design or just point me in the right direction? Any help greatly appreciated. Thanks

推荐答案

不管类仍保持连接的这个载体需要一个<一个href=\"http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/reference/io_service__strand.html\">strand.使用链::后链::调度访问时,增加,或从载体移除。该链概念进行了详细的文档中解释

Whatever class maintains this vector of connections needs a strand. Use strand::post or strand::dispatch when accessing, adding to, or removing from the vector. The strand concept is explained in detail in the documentation.

一个链被定义为一个严格
  事件的顺序调用
  处理程序(即没有并发
  调用)。股线的使用允许
  在多线程code的执行
  程序,而不需要明确的
  锁定(例如,使用互斥)。

A strand is defined as a strictly sequential invocation of event handlers (i.e. no concurrent invocation). Use of strands allows execution of code in a multithreaded program without the need for explicit locking (e.g. using mutexes).

这篇关于C ++多线程服务器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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