我正在boost::circular_buffer
中实施boost::managed_shared_memory
。我在共享内存here中使用了示例代码演示向量。我做了以下更改:
1) typedef boost::circular_buffer<int, ShmemAllocatorCB> MyCircularBuffer;
2) MyCircularBuffer *circbuff = segment.construct<MyCircularBuffer>("MyCB")(alloc_inst);
3) circbuff->push_back(1);
代码在第3行给出了编译错误。错误是
error C2665: 'operator new' : none of the 5 overloads could convert all the argument types c:\boost2005\boost\circular_buffer\base.hpp 1470
从documentation开始,我知道push_back函数需要以下三种形式之一:
void push_back(param_value_type);
void push_back(rvalue_type);
void push_back();
我尝试了空参数调用,尝试将1转换为param_value_type,rvalue_type但它们似乎都没有工作。这可能是一个简单的错误,但我现在已经有很长一段时间没有弄清楚了。任何帮助表示赞赏。感谢。
修改
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocatorCB;
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
const ShmemAllocatorCB alloc_inst (segment.get_segment_manager());
答案 0 :(得分:2)
看起来Boost的循环缓冲区在某个时间点使用原始指针进行调试。不幸的是,这是令人望而却步的:
Boost提供了一些关于这个主题的参考:
总之,通过定义BOOST_CB_DISABLE_DEBUG
或NDEBUG
预处理器定义来编译代码而不使用circular_buffer调试支持。
(使用BOOST_LIB_VERSION
= 1_49
测试)
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/circular_buffer.hpp>
#include <iostream>
#include <string>
#include <cstdlib> //std::system
using namespace boost::interprocess;
//Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
//This allocator will allow placing containers in the segment
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocatorCB;
//Alias a vector that uses the previous STL-like allocator so that allocates
//its values from the segment
typedef boost::circular_buffer<int, ShmemAllocatorCB> MyCircularBuffer;
//Main function. For parent process argc == 1, for child process argc == 2
int main(int argc, char *argv[])
{
if(argc == 1){ //Parent process
//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;
//Create a new segment with given name and size
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
//Initialize shared memory STL-compatible allocator
const ShmemAllocatorCB alloc_inst (segment.get_segment_manager());
//Construct a buffer named "MyCB" in shared memory with argument 10 (for size) and alloc_inst
MyCircularBuffer *circbuff = segment.construct<MyCircularBuffer>("MyCB")(10, alloc_inst);
for(int i = 0; i < 100; ++i) //Insert data in the buffer, with overflows
circbuff->push_back(i);
//Launch child process
std::string s(argv[0]); s += " child ";
if(0 != std::system(s.c_str()))
return 1;
//Check child has destroyed the buffer
if(segment.find<MyCircularBuffer>("MyCB").first)
return 1;
}
else{ //Child process
//Open the managed segment
managed_shared_memory segment(open_only, "MySharedMemory");
//Find the buffer using the c-string name
MyCircularBuffer *circbuff = segment.find<MyCircularBuffer>("MyCB").first;
//Use buffer in reverse order
std::cout << "Child got: " << (*circbuff)[3] << "\n"; // 93
//When done, destroy the buffer from the segment
segment.destroy<MyCircularBuffer>("MyCB");
}
return 0;
};
评估它:
g++ -DBOOST_CB_DISABLE_DEBUG -DNDEBUG cb_ipc.cpp -o cb_ipc -lboost_system -lrt
运行它:
./cb_ipc
Child got: 93
答案 1 :(得分:0)
这里的问题是您没有正确设置circular_buffer的容量。我认为容量为零,因此你无法推动。
构建circular_buffer后,将容量设置为所需的大小。
#define BOOST_CB_DISABLE_DEBUG
#include <boost/circular_buffer.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
using namespace boost;
using namespace boost::interprocess;
typedef allocator<T, managed_shared_memory::segment_manager> cb_alloc;
typedef circular_buffer<T, cb_alloc> cb;
managed_shared_memory* m_mem_segment;
cb* m_buffer;
// Create shared memory segment of a size able to hold the max updates.
m_mem_segment = new managed_shared_memory(open_or_create, NAMED_MEMORY, MEMORY_SIZE);
if (m_mem_segment == nullptr) {
return false;
}
// Get the segement manager for allocating a circular buffer.
const cb_alloc alloc_inst(m_mem_segment->get_segment_manager());
// Create a circular buffer in the shared memory segment.
m_buffer = m_mem_segment->find_or_construct<cb>(NAMED_BUFFER)(alloc_inst);
if (m_mem_segment == nullptr) {
return false;
}
// Set the capacity if not set (zero) (this must be set!)
if (m_buffer->capacity() <= 0) {
m_buffer->set_capacity(50);
}
你可以推到这么好,我测试了多个进程交换数据。 这是一个简单的例子,你显然必须在使用named_mutex或信号量等的进程之间实现自己的同步。