我正在学习boost的共享内存,以将其应用于我的OpenCV项目。 这是两个测试片段。
class.h
#ifndef _class_H_
#define _class_H_
class Myclass{
public:
Myclass() : number(0){}
int number;
};
#endif
write.cpp
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"
int main(int argc, char *argv[]){
using namespace boost::interprocess;
struct shm_remove{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
Myclass *instance = segment.construct<Myclass>("instance1")();
instance = new Myclass();
std::system("read.exe");
std::cout << instance->number << std::endl;
return 0;
}
read.cpp
#include <boost/interprocess/managed_shared_memory.hpp>
#include <cstdlib>
#include <iostream>
#include "include/class.h"
int main(int argc, char *argv[]){
using namespace boost::interprocess;
managed_shared_memory segment(open_only, "MySharedMemory");
std::pair<Myclass*, managed_shared_memory::size_type> res;
res = segment.find<Myclass> ("instance1");
res.first->number = 3;
return 0;
}
当然,输出必须为3,但必须为0,这表示未修改类。 如何修改共享内存中的对象?