我搜索在两个进程之间共享一个结构。但我没有成功。你能帮助理解吗?
以下是我的第一个流程代码:
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#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>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include "semaphore_shared_data.hpp"
using namespace boost::interprocess;
int main(){
shared_memory_object shm(open_or_create, "shared_memory", read_write);
shm.truncate(sizeof(shared_memory_buffer));
mapped_region region(shm,read_write);
void *addr = region.get_address();
shared_memory_buffer *data = new (addr) shared_memory_buffer;
while(true){
data->writer.wait();
std::cout << "Process 1 read: " << data->Variable.Type << ":" <<data->Variable.Value << std::endl;
data->Variable.Type = "ACK";
data->Variable.Value = 1;
sleep(1);
data->reader.post();
}
return 0;
}
这是我的第二个过程的代码:
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#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>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#include <iostream>
#include <math.h>
#include <string>
#include "semaphore_shared_data.hpp"
using namespace boost::interprocess;
int main ()
{
shared_memory_object shm(open_only, "shared_memory",read_write);
mapped_region region(shm,read_write);
void *addr = region.get_address();
shared_memory_buffer *data = static_cast<shared_memory_buffer*>(addr);
while(true)
{
data->reader.wait();
std::cout << "Process 2 read: " << data->Variable.Type << ":" << data->Variable.Value << std::endl;
data->Variable.Type = "ACK";
data->Variable.Value = 2;
data->writer.post();
}
return 0;
}
这是我的shared_memory_buffer代码(semaphore_shared_data.hpp)
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
#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>
#include <string>
using namespace boost::interprocess;
typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;
struct shared_Struct
{
shared_Struct():Type("ACK"), Value(0){}
shared_string Type;
float Value;
};
struct shared_memory_buffer
{
shared_memory_buffer(): writer(1), reader(0), Variable(){}
interprocess_semaphore writer, reader;
shared_Struct Variable;
};
我有这个错误:
semaphore_shared_data.hpp: In constructor ‘boost::container::basic_string<CharT, Traits, Allocator>::basic_string(const CharT*, const allocator_type&) [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >; boost::container::basic_string<CharT, Traits, Allocator>::allocator_type = boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >]’:
semaphore_shared_data.hpp:16:38: error: no matching function for call to ‘boost::interprocess::allocator<char, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::allocator()’ shared_Struct():Type("ACK"), Value(0){}
答案 0 :(得分:1)
我认为我的错误是由于这个声明:
typedef allocator<char, managed_shared_memory::segment_manager> CharAllocator;
typedef basic_string<char, std::char_traits<char>, CharAllocator>shared_string;
gcc表示:
shared_Struct():Type("ACK"), Value(0){}