我试图创建一个类管理的(STD)字符串共享内存载体。
typedef boost::interprocess::allocator<std::string, boost::interprocess::managed_shared_memory::segment_manager> shmem_allocator;
typedef boost::interprocess::vector<std::string, shmem_allocator> shmem_vector;
shmem_mgr::shmem_mgr() :
shmem_(create_only, SHMEM_KEY, SHMEM_SIZE),
allocator_(shmem_.get_segment_manager())
{
mutex_ = shmem_.find_or_construct<interprocess_mutex>(SHMEM_MUTEX)();
condition_ = shmem_.find_or_construct<interprocess_condition>(SHMEM_CONDITION)();
//buffer_ is of type shmem_vector
buffer_ = shmem_.construct<shmem_vector>(SHMEM_BUFFER_KEY)(allocator_);
}
void shmem_mgr::run() {
running_ = true;
while(running_) {
scoped_lock<interprocess_mutex> lock ( *mutex_ );
int size = buffer_->size();
log_.debug() << size << " queued request(s) found" << std::endl; //LINE 27
for(int i=0; i<size; i++) {
log_.debug() << buffer_->at(i); // at() crashes my app
}
buffer_->clear(); //so does clear()
condition_->wait (lock);
}
}
客户端成功增加了一个字符串到载体(它也成功读取来自缓冲器调试该字符串),则管理器(代码以上)接收的信号(condtion变量),写有在载体中的串(27行),但是当它试图通过获得该字符串at()
的应用程序崩溃。
编辑:我已经想通了,即使用
std::string
是不可能的,有一个
string
在提升IPC容器只是这种情况。 这不会改变,我需要的(升压/ STD)字符串向量的事实...
问 : 我如何可以通过跨共享内存字符串? 我需要将它们存储在一些缓冲液(能够在时间存储> 1)在SHMEM,然后在第二工艺取 - 那是必要条件。 输入总是std::string
等是输出,但在SHMEM内部表示可以是不同的。
从文档 。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
int main ()
{
using namespace boost::interprocess;
//Typedefs
typedef allocator<char, managed_shared_memory::segment_manager>
CharAllocator;
typedef basic_string<char, std::char_traits<char>, CharAllocator>
MyShmString;
typedef allocator<MyShmString, managed_shared_memory::segment_manager>
StringAllocator;
typedef vector<MyShmString, StringAllocator>
MyShmStringVector;
//Open shared memory
//Remove shared memory on construction and destruction
struct shm_remove
{
shm_remove() { shared_memory_object::remove("MySharedMemory"); }
~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }
} remover;
managed_shared_memory shm(create_only, "MySharedMemory", 10000);
//Create allocators
CharAllocator charallocator (shm.get_segment_manager());
StringAllocator stringallocator(shm.get_segment_manager());
//This string is in only in this process (the pointer pointing to the
//buffer that will hold the text is not in shared memory).
//But the buffer that will hold "this is my text" is allocated from
//shared memory
MyShmString mystring(charallocator);
mystring = "this is my text";
//This vector is only in this process (the pointer pointing to the
//buffer that will hold the MyShmString-s is not in shared memory).
//But the buffer that will hold 10 MyShmString-s is allocated from
//shared memory using StringAllocator. Since strings use a shared
//memory allocator (CharAllocator) the 10 buffers that hold
//"this is my text" text are also in shared memory.
MyShmStringVector myvector(stringallocator);
myvector.insert(myvector.begin(), 10, mystring);
//This vector is fully constructed in shared memory. All pointers
//buffers are constructed in the same shared memory segment
//This vector can be safely accessed from other processes.
MyShmStringVector *myshmvector =
shm.construct<MyShmStringVector>("myshmvector")(stringallocator);
myshmvector->insert(myshmvector->begin(), 10, mystring);
//Destroy vector. This will free all strings that the vector contains
shm.destroy_ptr(myshmvector);
return 0;
}
您需要为您共享STL类自定义分配器。 你需要一个自我基于指针(ACE&提升有这些)中的分配部定义。 在相对侧上(连续的)共享存储器通常驻留在不同的地址。 你需要一个共享内存分配子系统(堆管理器)太(该分配器分配从) - 所有非平凡的底层代码,但最肯定是可行的,一旦你拥有了它,它是可用的无处不在。 如果你做的一切,你只需要通过非平面结构的位移(从(CONTIGUOUS !!)堆区的开始)左右。
您可以创建队列和一切你可能想 - 所有提供的“指针”的对象是自我为基础,该DIS连续件在非平幅来自一个大的连续件。
您不能使用的std :: string,因为,除非你能掌控的分配,在标准串记忆无关,与你共享内存 - 同样为任何其他STL结构
此外,还必须(像往常一样)来解决/同意所有权问题
您可以使用boost ::进程间:: managed_shared_memory。 下面的程序经过2个进程之间一个boost ::进程间::字符串。 作品在我的机器(Ubuntu Linux操作系统)上的罚款。 您可以使用managed_shared_memory传递载体或对象。 提高::进程间::串有c_str()方法。
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
int main(int argc, char *argv[])
{
using namespace boost::interprocess;
typedef boost::interprocess::allocator<char, boost::interprocess::managed_shared_memory::segment_manager> CharAllocator;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, CharAllocator> string;
if(argc == 1){ //Parent process
boost::interprocess::shared_memory_object::remove("MySharedMemory");
//Create a shared memory object.
managed_shared_memory shm (create_only, "MySharedMemory", 1024);
string *s = shm.find_or_construct<string>("String")("Hello!", shm.get_segment_manager());
std::cout << *s << std::endl;
//Launch child process
std::string s1(argv[0]); s1 += " child ";
if(0 != std::system(s1.c_str()))
return 1;
}
else{
//Open already created shared memory object.
managed_shared_memory shm (open_only, "MySharedMemory");
std::pair<string *,std::size_t> ret = shm.find<string>("String");
std::cout << *(ret.first) << std::endl;
}
return 0;
}