在C ++中父进程和子进程之间共享队列 [英] share queue between parent and child process in c++

查看:172
本文介绍了在C ++中父进程和子进程之间共享队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多方法可以处理两个进程之间的相互通信,但是我还是有点困惑如何处理它.是否可以高效地在两个进程之间共享队列(来自标准库)?

I know there are many way to handle inter-communication between two processes, but I'm still a bit confused how to deal with it. Is it possible to share queue (from standard library) between two processes in efficient way?

谢谢

推荐答案

简单的答案:可以通过两个进程共享std::queue,但这并不容易.

Simple answer: Sharing an std::queue by two processes can be done but it is not trivial to do.

您可以使用共享内存将队列与某些同步机制(通常是互斥锁)保持在一起.请注意,不仅必须在共享内存区域中构造std::queue对象,而且还必须在队列中构造该对象,因此您将必须提供自己的分配器来管理共享区域中的内存创建.

You can use shared memory to hold the queue together with some synchronization mechanism (usually a mutex). Note that not only the std::queue object must be constructed in the shared memory region, but also the contents of the queue, so you will have to provide your own allocator that manages the creation of memory in the shared region.

如果可以,请尝试查看可能为您的过程通信需求提供已打包解决方案的更高级别的库.考虑 Boost.Interprocess 或在您喜欢的搜索中搜索进程间通信引擎.

If you can, try to look at higher level libraries that might provide already packed solutions to your process communication needs. Consider Boost.Interprocess or search in your favorite search engine for interprocess communication.

这篇关于在C ++中父进程和子进程之间共享队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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