gpt4 book ai didi

C++多进程?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:55:22 25 4
gpt4 key购买 nike

我有一个由两个进程组成的项目,我需要以快速高效的方式在它们之间传递一些数据。

我知道我可以使用套接字通过 TCP 来执行此操作,即使这两个进程将始终存在于同一台计算机上,但这似乎不是一个非常有效的解决方案。

我看到很多关于在 Linux 上使用“管道”的信息。但是,我主要希望它适用于 Windows 和 Linux(最好通过跨平台库),最好是类型安全的,非阻塞方式。

另一件重要的事情是我需要支持整个应用程序的多个实例(即两个进程),每个实例都有自己独立的通信对象拷贝。

是否还有一种跨平台的方式来生成新进程?

最佳答案

看看Boost.Interprocess

Boost.Interprocess simplifies the use of common interprocess communication and synchronization mechanisms and offers a wide range of them:

  • Shared memory.
  • Memory-mapped files.
  • Semaphores, mutexes, condition variables and upgradable mutex types to place them in shared memory and memory mapped files.
  • Named versions of those synchronization objects, similar to UNIX/Windows sem_open/CreateSemaphore API.
  • File locking.
  • Relative pointers.
  • Message queues.

Boost.Interprocess also offers higher-level interprocess mechanisms to allocate dynamically portions of a shared memory or a memory mapped file (in general, to allocate portions of a fixed size memory segment). Using these mechanisms, Boost.Interprocess offers useful tools to construct C++ objects, including STL-like containers, in shared memory and memory mapped files:

  • Dynamic creation of anonymous and named objects in a shared memory or memory mapped file.
  • STL-like containers compatible with shared memory/memory-mapped files.
  • STL-like allocators ready for shared memory/memory-mapped files implementing several memory allocation patterns (like pooling).

Boost.Interprocess has been tested in the following compilers/platforms:

  • Visual 7.1 Windows XP
  • Visual 8.0 Windows XP
  • GCC 4.1.1 MinGW
  • GCC 3.4.4 Cygwin
  • Intel 9.1 Windows XP
  • GCC 4.1.2 Linux
  • GCC 3.4.3 Solaris 11
  • GCC 4.0 MacOs 10.4.1

关于C++多进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1235299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com