Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ asio、共享内存和进程间通信_C++_Boost_Boost Asio_Boost Interprocess - Fatal编程技术网

C++ asio、共享内存和进程间通信

C++ asio、共享内存和进程间通信,c++,boost,boost-asio,boost-interprocess,C++,Boost,Boost Asio,Boost Interprocess,我有一个专门使用boost::asio作为输入数据源的应用程序,因为我们的大多数对象都是基于网络通信的。由于一些特定的需求,我们现在也需要使用共享内存作为输入方法的能力。我已经编写了共享内存组件,它运行得比较好 问题是如何处理从共享内存进程到消费应用程序的通知,即可以读取数据——我们需要处理现有输入线程中的数据(使用boost::asio),并且我们也不需要阻止等待数据的输入线程 我通过引入一个中间线程来实现这一点,该线程等待从共享内存提供程序进程发出信号的事件,然后向输入线程发布一个完成处理程

我有一个专门使用
boost::asio
作为输入数据源的应用程序,因为我们的大多数对象都是基于网络通信的。由于一些特定的需求,我们现在也需要使用共享内存作为输入方法的能力。我已经编写了共享内存组件,它运行得比较好

问题是如何处理从共享内存进程到消费应用程序的通知,即可以读取数据——我们需要处理现有输入线程中的数据(使用
boost::asio
),并且我们也不需要阻止等待数据的输入线程

我通过引入一个中间线程来实现这一点,该线程等待从共享内存提供程序进程发出信号的事件,然后向输入线程发布一个完成处理程序来处理数据读取

这一点现在也在起作用,但中间线程的引入意味着在很多情况下,我们在读取数据之前有一个额外的上下文切换,这对延迟有负面影响,并且额外线程的开销也相对昂贵

下面是一个应用程序正在执行的简单示例:

#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);
   }
}
#包括
使用名称空间std;
#包括
#包括
#包括
#包括
类简单线程
{
公众:
简单线程(const std::string和name)
:姓名(姓名)
{}
void start()
{
线程重置(新的boost::线程(
boost::bind(&simple_-thread::run,this));
}
私人:
虚拟void do_run()=0;
无效运行()
{

cout我想你已经找到了答案,因为你发布了这个问题,这是为了其他人的利益

试试看boost

它使您能够选择要在哪个线程上执行某些工作

它将自动在特定的串上排队,这是您不必考虑的事情


如果您需要知道工作何时完成,它甚至会为您提供一个完成处理程序。

如果您在*NIX上,最简单的方法是使用UNIX管道进行通知,因此管道的一端作为常规套接字添加到
输入线程中。您仍然会产生同步等开销,但会保存一个冗余副本(到套接字缓冲区/从套接字缓冲区中)。您可以通过套接字发送偏移量和长度,然后直接索引shmem。在Windows中,您可以使用Windows::object\u句柄直接对同步对象进行异步等待。