eme*_*esx 8 c++ boost shared-memory boost-interprocess
我正在尝试创建一个管理(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);
}
}
Run Code Online (Sandbox Code Playgroud)
客户端成功地向向量添加一个字符串(它也成功从缓冲区读取该字符串以进行调试),管理器(上面的代码)接收信号(condtion变量),写入向量中有一个字符串(第27行) ),但当它试图通过at()应用程序崩溃获取该字符串.
std::string是不可能的,string在这种情况下,boost ipc中有一个容器.这并没有改变我需要(boost/std)字符串向量的事实......
问:如何在共享内存中传递字符串?我需要将它们存储在某个缓冲区(能够一次存储> 1)中,然后在第二个进程中获取 - 这是要求.输入始终std::string是输出,输出也是如此,但shmem中的内部表示可能不同.
car*_*org 13
来自文档.
#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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8502 次 |
| 最近记录: |