非螺纹替代等待条件。 (编辑:用Boost.Asio的摄器模式?) [英] Non-threaded alternative to waiting on a condition. (Edit: Proactor pattern with boost.asio?)

查看:121
本文介绍了非螺纹替代等待条件。 (编辑:用Boost.Asio的摄器模式?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现一个消息传递算法。消息相邻节点之间传递时,他们有在节点足够的信息来撰写邮件 - 传递到节点来自相邻节点的信息。实现很简单,如果我使每消息的线程和使用的boost ::条件把线程睡眠,直到所需的资料。

I am implementing a message passing algorithm. Messages pass between adjacent nodes when they have enough information at the node to compose the message - information that is passed to the node from neighbouring nodes. The implementation is trivial if I make each of messages a thread and use boost::condition to put the thread to sleep until the required information is available.

不幸的是 - 我有这将意味着30万线图100K节点。当我asked如何使许多线程的答案是,我不应该 - 与重新设计来代替。

Unfortunately - I have 100k nodes in the graph which would mean 300k threads. When I asked how to make that many threads the answer was that I shouldn't - and re-design instead.

我的问题是:是否有以等待条件的标准设计模式
也许有些异步控制模式?

My question is: is there a standard design pattern for waiting for a condition? Perhaps some asynchronous control pattern?

编辑:我想我可以用proacator模式做到这一点。
我已经编辑,标记包含的boost :: ASIO - 看看是否有人有这样的建议。

I think I can do this with the proacator pattern. I have edited to tags to include boost::asio - to see if anyone has suggestions with this.

所以,讨论可在混凝土,这里的信息是如何到目前为止定义的:

So the discussion can be concrete, here is how the messages are defined so far:

class
Message
{
public:      
  Message(const Node* from, Node* to)
    : m_from(from), m_to(to)
  {}
  void
  operator()()
  {
    m_to->ReceiveMessage( m_from->ComposeMessage() );
  }
private:
  Node *m_from, *m_to;
};

这些消息函子正与升压::线程启动。然后我们有

These message functors are currently launched with boost::thread. Then we have

class Node
{
    Node(Node* Neighbour1, Node* Neighbour2, Node* Neighbour3); 
   // The messages (currently threads) are created on construction, 
   // The condition locks then sort out when they actually get passed 
   // without me having to think too hard.

    void ReceiveMessage(const Message&); 
    //set m_message from received messages;
    //EDIT This looks like an async write - use boost asio here?

    Message 
    ComposeMessage()
    {
      // If possible I want to implement this function without threads
      // It works great but it if every message is a thread 
      // then I have 300k threads.
      // EDIT: this looks like an async read (use boost asio here?)

      boost::mutex::scoped_lock lock(m_mutex);
       while (!m_message) //lock the thread until parameter is set.
        m_cond.wait(lock);
      return *m_message;
   }
  private:
    boost::optional<Message> m_message;
    boost::mutex m_mutex;
    boost::condition m_cond;
}

我喜欢code的透明度,如果可能的话想通过必须有条件锁定一些替代以保持相同的接口?

I like the transparency of the code and if possible would like to keep the same interfaces by having some alternative to the conditional lock?

推荐答案

我猜你正在寻找的是的反应堆模式。这是大多数活动并不需要太多的时间,他们正在做的协同式多任务。请参见的node.js 一个JavaScript实现的想法,但在C ++中的 ACE库提供了这个概念外的开箱允许基于内核的系统数量多线程。

I guess what you are looking for is the reactor pattern. This is where most of the activities do not take too much time and they are doing co-operative multitasking. See node.js for a JavaScript implementation of the idea, but in C++ the ACE library provides this concept out-of-the-box allowing multiple threads based on the number of cores in the system.

这些库都依赖于支持磁盘,网络等非阻塞IO时,你是不是等待OS操作系统的一些API的,但在你的应用程序的另一个消息源,他们为你提供工具这一点。

These libraries all depend on some OS APIs that support non-blocking IO on disks, network, etc. When you are not waiting for the OS, but another message source in your app, they provide you with the tools for that.

这篇关于非螺纹替代等待条件。 (编辑:用Boost.Asio的摄器模式?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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