boost::interprocess Containers of containers 유형의 예제 데모 프로그램이 있습니다. 그러나 클래스를 내 프로세스 메모리 내에서 정상 클래스로 사용하는 것을 좋아합니다. 누군가가 현재 프로세스 메모리에서 초기화 된 클래스를 갖기 위해 인수를 취하지 않는 생성자를 작성할 수있게 도와 줄 수 있습니까?boost :: interprocess 공유 메모리가 아닌 컨테이너 컨테이너
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <shmfw/serialization/interprocess_vector.hpp>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace boost::interprocess;
//Alias an STL-like allocator of ints that allocates ints from the segment
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
//Alias a vector that uses the previous STL-like allocator
typedef vector<int, ShmemAllocator> MyVector;
typedef allocator<void, managed_shared_memory::segment_manager > void_allocator;
class MyStruct {
public:
MyVector myVector;
//Since void_allocator is convertible to any other allocator<T>, we can simplify
//the initialization taking just one allocator for all inner containers.
MyStruct (const void_allocator &void_alloc)
: myVector (void_alloc)
{}
// Thats what I like to have
//MyStruct()
// : myVector (??)
//{}
};
int main() {
// I would like to have something like that working and also the shm stuff below
// MyStruct x;
managed_shared_memory segment;
//A managed shared memory where we can construct objects
//associated with a c-string
try {
segment = managed_shared_memory(create_only, "MySharedMemory", 65536);
} catch (...){
segment = managed_shared_memory(open_only, "MySharedMemory");
}
//Initialize the STL-like allocator
const ShmemAllocator alloc_inst (segment.get_segment_manager());
MyStruct *myStruct_src = segment.find_or_construct<MyStruct> ("MyStruct") (alloc_inst);
srand (time(NULL));
myStruct_src->myVector.push_back (rand());
MyStruct *myStruct_des = segment.find_or_construct<MyStruct> ("MyStruct") (alloc_inst);
for (size_t i = 0; i < myStruct_src->myVector.size(); i++) {
std::cout << i << ": " << myStruct_src->myVector[i] << " = " << myStruct_des->myVector[i] << std::endl;
if(myStruct_src->myVector[i] != myStruct_des->myVector[i]) {
std::cout << "Something went wrong!" << std::endl;
}
}
//segment.destroy<MyVector> ("MyVector");
return 0;
}
다양한 솔루션을, 감사 – Max
을 사람이 scoped_allocator로이 답변을 결합하는 방법을 알고 있나요? -> 질문은 http://stackoverflow.com/q/28608185/1358042에 자세히 설명되어 있습니다. 감사합니다. – Max