简体   繁体   English

Boost :: asio,共享内存和进程间通信

[英]Boost::asio, Shared Memory and Interprocess Communication

I have an application that is written to use boost::asio exclusively as its source of input data as most of our objects are network communication based. 我有一个应用程序编写为使用boost::asio作为其输入数据源,因为我们的大多数对象都是基于网络通信。 Due to some specific requirements, we now require the ability to use shared memory as an input method as well. 由于某些特定要求,我们现在还要求使用共享内存作为输入方法。 I've already written the shared memory component and it is working relatively well. 我已经编写了共享内存组件,它的工作相对较好。

The problem is how to handle notifications from the shared memory process to the consuming application that data is available to be read -- we need to handle the data in the existing input thread (using boost::asio ), and we also need to not block that input thread waiting for data. 问题是如何处理从共享内存进程到消费应用程序的数据可供读取的通知 - 我们需要处理现有输入线程中的数据(使用boost::asio ),我们还需要阻止输入线程等待数据。

I've implemented this by introducing an intermediate thread that waits on events to be signaled from the shared memory provider process then posts a completion handler to the input thread to handle reading in the data. 我通过引入一个中间线程来实现这一点,该线程等待从共享内存提供程序进程发出信号的事件,然后将完成处理程序发布到输入线程以处理数据中的读取。

This is working now also, but the introduction of the intermediate thread means that in a significant amount of cases we have an extra context switch before we can read the data which has a negative impact on latency, and the overhead of the additional thread is also relatively expensive. 现在这也正常工作,但是中间线程的引入意味着在大量的情况下我们有一个额外的上下文切换,然后我们才能读取对延迟有负面影响的数据,并且附加线程的开销也是相对昂贵。

Here's a simplistic example of what the application is doing: 这是应用程序正在做的简单示例:

#include <iostream>
using namespace std;

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/bind.hpp>

class simple_thread
{
public:
   simple_thread(const std::string& name)
      : name_(name)
   {}

   void start()
   {
      thread_.reset(new boost::thread(
         boost::bind(&simple_thread::run, this)));
   }

private:
   virtual void do_run() = 0;

   void run()
   {
      cout << "Started " << name_ << " thread as: " << thread_->get_id() << "\n";
      do_run();
   }


protected:
   boost::scoped_ptr<boost::thread> thread_;
   std::string name_;
};

class input_thread
   : public simple_thread
{
public:
   input_thread() : simple_thread("Input")
   {}

   boost::asio::io_service& svc()
   {
      return svc_;
   }

   void do_run()
   {
      boost::system::error_code e;
      boost::asio::io_service::work w(svc_);
      svc_.run(e);
   }

private:
   boost::asio::io_service svc_;
};

struct dot
{
   void operator()()
   {
      cout << '.';
   }
};

class interrupt_thread
   : public simple_thread
{
public:
   interrupt_thread(input_thread& input)
      : simple_thread("Interrupt")
      , input_(input)
   {}

   void do_run()
   {
      do
      {
         boost::this_thread::sleep(boost::posix_time::milliseconds(500));
         input_.svc().post(dot());
      }
      while(true);
   }

private:
   input_thread& input_;
};

int main()
{
   input_thread inp;
   interrupt_thread intr(inp);

   inp.start();
   intr.start();

   while(true)
   {
      Sleep(1000);
   }
}

Is there any way to get the data handled in the input_thread directly (without having to post it in via the interrupt_thread ? The assumption is that the interrupt thread is totally driven by timings from an external application (notification that data is available via a semaphore). Also, assume that we have total control of both the consuming and providing applications, that we have additional objects that need to be handled by the input_thread object (so we cannot simply block and wait on the semaphore objects there). The goal is to reduce the overhead, CPU utilization and latency of the data coming in via the shared memory providing application. 有没有什么办法让在处理的数据input_thread直接(而无需post通过它interrupt_thread ?的假设是,中断线程中定时通过信号灯完全驱动从外部应用程序(通知数据可用)另外,假设我们完全控制了消费和提供应用程序,我们还有其他需要由input_thread对象处理的对象(所以我们不能简单地阻塞并等待那里的信号量对象)。目标是通过共享内存提供应用程序减少数据的开销,CPU利用率和延迟。

I guess you have found your answer since you posted this question, this is for others benefit... 我猜你已经找到了答案,因为你发布了这个问题,这是为了别人的利益......

try and check out boost strands . 尝试检查提升

It gives you the ability to choose on which thread you want to do some work on. 它使您能够选择要在哪个线程上进行的工作。

It will automatically get queued on the specific strand, that's something you won't have to think about. 它将自动排在特定的链上,这是你不必考虑的事情。

It even gives you a completion handler if you need to know when the work is done. 如果您需要知道工作何时完成,它甚至会为您提供完成处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM