我想使用共享内存技术在进程之间共享数据。我可以分别在 Windows 和 WSL(Linux 的 Windows 子系统)上使用 boost 库来做到这一点。两者都很好用。我的工作是让这些脚本在 1 个进程在 Windows 上运行,1 个进程在 WSL Linux 上运行时工作。他们在同一台机器上运行。
发件人脚本
#include <chrono>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <cstring>
#include <iostream>
#include <thread>
using namespace boost::interprocess;
int main(int argc, char* argv[])
{
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("sharedmem"); }
~shm_remove() { shared_memory_object::remove("sharedmem"); }
} remover;
//Create a shared memory object.
shared_memory_object shm(create_only, "sharedmem", read_write);
//Set size
shm.truncate(1000);
//Map the whole shared memory in this process
mapped_region …Run Code Online (Sandbox Code Playgroud)