gpt4 book ai didi

C++ Boost::Interprocess 共享内存与结构

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:33 25 4
gpt4 key购买 nike

我搜索在两个进程之间共享一个结构。但我没有成功。你能帮忙理解吗?

这是我的第一个过程的代码:

#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){}

最佳答案

我认为我的错误是由于这个声明:

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){}

关于C++ Boost::Interprocess 共享内存与结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26122973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com