- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想实现一些消息队列(基于 vector )以某种方式处理来自网络的数据,为此我使用共享内存来保存消息,但遇到了与之相关的问题,问题是我的代码有效好吧,当我第一次运行它时,当我想再次运行它时,当我想为共享内存中队列中的字符串分配一个新值时,实际上是在我想移动它时(同样的问题)当我想复制它时存在)。当 SSO 工作时,问题不存在,所以当我有足够小的字符串时。我做错了什么?
#include <atomic>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bip = boost::interprocess;
struct BadSharedMemoryAccess final : public std::exception
{
BadSharedMemoryAccess(std::string&& msg):
msg_{std::move(msg)}
{}
virtual const char* what() const noexcept
{
return msg_.c_str();
}
private:
std::string msg_;
};
struct Message
{
bip::string message_;
};
template<typename Alloc>
class MyCustomData final
{
public:
using allocator_type = typename Alloc::template rebind<Message>::other;
MyCustomData(std::size_t number_of_messages, Alloc alloc = {}) :
init_add_index_{0},
init_handle_index_{-1},
messages_{number_of_messages, alloc}
{}
public:
uint_fast64_t init_add_index_;
int_fast64_t init_handle_index_;
std::vector<Message, Alloc> messages_;
// bip::vector<data::Message, Alloc> messages_;
};
template<typename DataType, typename DataAllocator>
class SharedMemory
{
public:
template<typename... Args>
SharedMemory(std::string const& shm_segment_name, std::size_t const segment_size,
std::string const& shm_object_name, Args&&... args) :
shm_object_name_{shm_object_name}
{
std::cout << "attempt to allocate space for shared memory segment " << shm_segment_name
<< ", size: ." << segment_size << std::endl;
setSharedMemorySize(shm_segment_name, segment_size);
DataAllocator const allocInstance{shm_.get_segment_manager()};
data_ = shm_.find_or_construct<DataType>(shm_object_name.c_str())(std::forward<Args>(args)..., allocInstance);
if (data_)
std::cout << "shared memory segment has been allocated" << std::endl;
else
std::cout << "shared memory has not been constructed or founded" << std::endl;
}
virtual ~SharedMemory()
{
std::cout << "shared memory segment will be closed." << std::endl;
}
void setSharedMemorySize(std::string const& shm_segment_name, std::size_t const segment_size)
{
auto page_size = bip::mapped_region::get_page_size();
auto const page_increase_rate{2};
while (page_size < segment_size)
{
page_size *= page_increase_rate;
}
std::cout <<"seting page size: " << page_size << std::endl;
shm_ = bip::managed_shared_memory{bip::open_or_create, shm_segment_name.c_str(), page_size};
std::cout << "space for shared memory has been successfully allocated." << std::endl;
}
DataType& getData()
{
if (not data_)
throw BadSharedMemoryAccess{"cannot access " + shm_object_name_};
return *data_;
}
protected:
DataType* data_;
private:
std::string const shm_object_name_;
bip::managed_shared_memory shm_;
};
namespace sharable
{
using DataAllocator = bip::allocator<Message, bip::managed_shared_memory::segment_manager>;
template<typename Alloc>
using DataType = MyCustomData<Alloc>;
}
int main()
{
std::size_t const max_number_of_elements_in_container{1000000};
auto shmem_data = std::make_shared<SharedMemory<MyCustomData<sharable::DataAllocator>, sharable::DataAllocator>>(
"SHM_SEGMENT", sizeof(MyCustomData<sharable::DataAllocator>) +
(max_number_of_elements_in_container * sizeof(Message) * 2),
"SHM_CONTAINER", max_number_of_elements_in_container);
std::vector<bip::string> feed{max_number_of_elements_in_container};
for (std::size_t i = 0; i < max_number_of_elements_in_container; ++i)
{
std::string s{"blablabla11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" + std::to_string(i)};
feed[i] = s.c_str();
}
auto& data = shmem_data->getData();
auto& shmem_vec = data.messages_;
std::cout << "addr: " << shmem_vec.data() << std::endl;
for (std::size_t i = 0; i < max_number_of_elements_in_container; ++i)
{
// if (i == 0)
// std::cout << "msg: " << shmem_vec[i].message_ << std::endl;
auto msg = feed[i];
shmem_vec[i].message_ = std::move(msg);
}
return 0;
}
最佳答案
scoped_allocator_adaptor
- 可以减轻一些痛苦,例如见SharedMemory
使用分配器键入?我的意思是,SharedMemory
应该是负责选择和传递权利的单点make_shared
)。Message
struct,而不是分配的字符串数据。你好像有for (std::size_t i = 0; i < max_number_of_elements_in_container; ++i) {
auto msg = feed[i];
shmem_vec[i].message_ = std::move(msg);
}
那很困惑。如果您这样做有什么好处(如果有效,请参见下文) auto msg = feed[i];
uint_fast64_t init_add_index_;
int_fast64_t init_handle_index_;
看起来好像您可能计划从多个同时使用这些atomic<>
至少类型。basic_string<>::swap
指定
The behavior is undefined if
Allocator
does not propagate on swap and theallocators of*this
andother
are unequal.
constant. If alloc is given and alloc != other.get_allocator(), then linear
basic_string<>
是容器,类似于
std::vector<>
)时分配器的语义更复杂:
#include <exception>
#include <iomanip>
#include <iostream>
#include <random>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
namespace bip = boost::interprocess;
struct BadSharedMemoryAccess final : std::runtime_error {
BadSharedMemoryAccess(std::string msg) : std::runtime_error{ std::move(msg) } {}
};
这就是前奏。现在,让我们陈述我们的意图:
using Segment = bip::managed_shared_memory;
template <typename U> using Alloc = bip::allocator<U, Segment::segment_manager>;
这使得引用(并可能切换出)该段及其分配器变得容易。
using Message = bip::string;
using Feed = bip::vector<Message>;
using SharedMessage = bip::basic_string<char, std::char_traits<char>, Alloc<char> >;
using SharedFeed = bip::vector<SharedMessage, Alloc<SharedMessage> >;
只需定义我们的域实体。
bip::string
/
bip::vector
对于堆和共享分配版本,我们获得了两者之间最好的互操作性;
class MyCustomData final {
public:
using allocator_type = SharedFeed::allocator_type;
MyCustomData(std::size_t capacity, allocator_type alloc)
: messages_(capacity, SharedMessage(alloc), alloc) // don't brace initlaize
{ }
auto& messages() { return messages_; }
auto const& messages() const { return messages_; }
private:
uint_fast64_t init_add_index_ = 0;
int_fast64_t init_handle_index_ = -1;
SharedFeed messages_;
};
For now, dropped the
virtual
destructor, and theMessage
struct that simply wrapped abip::string
for convenience.
template <typename T> class SharedMemory final {
public:
template <typename... Args>
SharedMemory(std::string const& shm_segment_name,
std::size_t const segment_size,
std::string const& shm_object_name,
Args&&... args)
: shm_ { bip::open_or_create, shm_segment_name.c_str(), segment_size }
{
data_ = shm_.find_or_construct<T>
(shm_object_name.c_str())
(std::forward<Args>(args)...,
shm_.get_segment_manager())
;
if (!data_) throw BadSharedMemoryAccess {"cannot access " + shm_segment_name + "/" + shm_object_name};
}
T const& get() const { return *data_; }
T& get() { return *data_; }
auto free() const { return shm_.get_free_memory(); }
protected:
T* data_;
private:
Segment shm_;
};
It strikes me that
SharedMemory
has too many responsibilities: on the onehand it tries to be a "smart-reference" for shared objects, and on the otherhand it "manages a segment". This leads to problems if you actually wanted tohave multiple objects in a segment. Consider splitting intoShared::Segment
andShared::Object<T>
.
Feed generate_heap_feed(size_t n) {
Feed feed;
feed.reserve(n);
for (size_t i = 0; i < n ; ++i) {
feed.emplace_back(
"blablabla11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
+ std::to_string(i));
}
return feed;
}
从
main
中提取测试馈送生成器.
int main() {
static constexpr std::size_t capacity { 1000000 };
static constexpr auto estimate = 300ull << 20; // 300 MiB (<< 10 kilo, << 20 mebi, << 30 gibi)
用慷慨的估计取代了被误导的计算³。请参阅下面的测量值。
using SharedData = SharedMemory<MyCustomData>;
SharedData shmem_data("SHM_SEGMENT", estimate, "SHM_CONTAINER", capacity);
std::cout << "Free: " << shmem_data.free() << "\n";
好看且可读。在我的系统上打印
"Free: 282572448"
第一次运行。
Feed const feed = generate_heap_feed(capacity);
SharedFeed& shm_feed = shmem_data.get().messages();
现在我们有我们的提要,让我们复制:
// copy feed from heap to shm
auto const n = std::min(feed.size(), shm_feed.size());
std::copy_n(feed.begin(), n, shm_feed.begin());
std::cout << "Copied: " << n << "\n";
std::cout << "Free: " << shmem_data.free() << "\n";
就这样。我们不尝试移动,因为我们知道那行不通。
bip::basic_string
正确知道如何在不兼容的分配器之间进行复制。没有汗水。
{
// check some random samples
std::default_random_engine prng{std::random_device{}()};
auto pick = [&] { return std::uniform_int_distribution<>(0, n-1)(prng); };
for (auto index : {pick(), pick(), pick(), pick()}) {
std::string_view a = feed.at(index);
std::string_view b = shm_feed.at(index);
std::cout << "Message #" << index
<< (a == b? " OK":" FAIL")
<< " " << std::quoted(b) << std::endl;
}
}
}
看看
Live On Coliru⁴
Especially note the filesize measurements (
--apparent-size
vs. the size ondisk). This confirms my point about sparse allocation. Even if you reserved100TB, the effective size of the SHM_CONTAINER would still be182MiB.
template <typename U> using Alloc = bip::allocator<U, Segment::segment_manager>;
和
template <typename U> using Alloc = boost::container::scoped_allocator_adaptor<
bip::allocator<U, Segment::segment_manager> >;
有诀窍,解锁神奇的分配器传播,例如从 vector 到
emplace
或
assign
)。所以我们可以
copy_n
更多来自:
// copy feed from heap to shm
auto const n = std::min(feed.size(), shm_feed.size());
std::copy_n(feed.begin(), n, shm_feed.begin());
std::cout << "Copied: " << n << "\n";
简单地说:
shm_feed.assign(feed.begin(), feed.end());
std::cout << "Copied: " << shm_feed.size() << "\n";
它具有与以前完全相同的分配行为。
This is a key point and a big bummer. Arthur O'Dwyer's Towards meaningful fancy pointers paper explores the territory, as does his book "Mastering the c++17 STL"
Message
再构造?
template <typename Alloc>
struct BasicMessage {
// pre-c++17:
// using allocator_type = typename Alloc::template rebind<char>::other;
using allocator_type = typename std::allocator_traits<Alloc>::template rebind_alloc<char>;
BasicMessage(std::allocator_arg_t, allocator_type alloc)
: _msg(alloc) { }
template <typename T1, typename... T,
typename = std::enable_if_t<
not std::is_same_v<std::allocator_arg_t, std::decay_t<T1> >
>
>
explicit BasicMessage(T1&& a, T&&... init)
: _msg(std::forward<T1>(a), std::forward<T>(init)...) { }
template <typename OtherAlloc>
BasicMessage(BasicMessage<OtherAlloc> const& other, allocator_type alloc)
: _msg(other.message().begin(), other.message().end(), alloc) { }
template <typename OtherAlloc, typename OM = BasicMessage<OtherAlloc> >
std::enable_if_t<
not std::is_same_v<allocator_type, typename OM::allocator_type>,
BasicMessage&>
operator=(BasicMessage<OtherAlloc> const& other) {
_msg.assign(other.message().begin(), other.message().end());
return *this;
}
template <typename OtherAlloc>
BasicMessage(std::allocator_arg_t, allocator_type alloc, BasicMessage<OtherAlloc> const& other)
: _msg(other.message().begin(), other.message().end(), alloc) { }
BasicMessage(BasicMessage const&) = default;
BasicMessage(BasicMessage&&) = default;
BasicMessage& operator=(BasicMessage const&) = default;
BasicMessage& operator=(BasicMessage&&) = default;
auto& message() const { return _msg; }
auto& message() { return _msg; }
private:
bip::basic_string<char, std::char_traits<char>, allocator_type> _msg;
};
using Message = BasicMessage<std::allocator<char> >;
using Feed = bip::vector<Message>;
using SharedMessage = BasicMessage<Alloc<char> >;
using SharedFeed = bip::vector<SharedMessage, Alloc<SharedMessage> >;
从好的方面来说,由于
: messages_(capacity, SharedMessage(std::allocator_arg, alloc), alloc) // don't brace initlaize
和
std::string_view a = feed.at(index).message();
std::string_view b = shm_feed.at(index).message();
一切仍然有效,请参阅
Live On Coliru
managed_shared_memory
与
manage_mapped_file
由于 Coliru 限制,容量减少
关于c++ - 在共享内存中移动 boost::interprocess::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62640914/
有没有人尝试创建进程间通信的日志文件?有人可以就实现这一目标的最佳方式给我一些建议吗? 最佳答案 这个问题不太清楚,评论使它不太清楚,但无论如何...... 首先要尝试的两件事是 ipcs和 stra
我使用boost::interprocess在共享内存中创建了boost::multi_index数据结构。有许多客户端进程将访问此数据结构。访问时,我将锁定数据结构。我遇到的问题是,一旦客户端进程正
我正在尝试使用 this 创建内存映射文件回答,但我收到编译错误。这是我的代码: namespace bi = boost::interprocess; std::string vecFile = "
我正在编写实时数据以增加共享内存。最初我每次想访问 shm 时都使用它: boost::interprocess::managed_shared_memory segment(boost::inte
我正在查看两个进程共享互斥锁和条件变量的 Boost 示例代码: https://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/synchro
我刚刚读了this page Boost.Interprocess 文档。这似乎表明,为了适应不同操作系统之间的差异并达成某种共识,某些进程间机制并没有使用操作系统提供的直接对应的本地机制来实现,而是
我有以下崩溃的代码。我怀疑这是因为分配了对堆栈的引用,所以我遇到了这个问题。但我想避免每次都必须堆栈分配互斥锁和作用域锁的成本 class Cache { public: void cr
在 Boost.Interprocess 文档中 Where is this being allocated?据称 Boost.Interprocess 容器同时使用两种机制放置在共享内存中: Boo
我已经在这个问题上待了好几天(甚至在 boost 论坛上的 posted)并且能够让第二个进程识别锁定的互斥锁似乎不起作用。请帮忙。这是代码: 通用头文件:SharedObject.hpp #ifnd
我正在使用 boost::interprocess::named_upgradable_mutex 来同步一些进程。 我正在使用 boost::interprocess::sharable_lock
我搜索在两个进程之间共享一个结构。但我没有成功。你能帮忙理解吗? 这是我的第一个过程的代码: #include #include #include #include #include #in
我正在移植源代码以打开/读取/写入在多个进程之间共享的文件。它在 Windows 下运行良好,因为它主要使用 boost::interprocess (1.44),我没想到会有太多问题,但我发现了一些
我想我终于掌握了 boost:interprocess 库的基础知识,并且在处理包含一些全是标准数据类型的成员变量的相对简单的类时,我一直在成功地使用它。 但是,我现在面临着将一个相当复杂的类推送到进
现在查看此链接: http://www.boost.org/doc/libs/1_56_0/doc/html/interprocess/quick_guide.html#interprocess.qu
我想用 boost 编写一个简单的应用程序,将字符串对象传递给其他进程。它编译得很好,但是当我尝试从第二个进程打印出字符串时,以下消息被发送到控制台并且第二个进程崩溃: ../boost_1_44_0
目前,我有 2 个进程使用 message_queue 和 shared_memory 表单 boost 进行通信。一切如常。 现在我需要使这个进程中的一个成为多线程的(再次感谢 boost),我想知
我需要围绕一个硬件进行进程间同步。因为此代码需要在 Windows 和 Linux 上运行,所以我使用 Boost 进程间互斥锁进行封装。一切正常接受我检查互斥量放弃的方法。这有可能发生,所以我必须为
这是一段我用来在共享内存上分配映射的代码,我正在使用boost::interprocess和托管共享内存段,现在的问题是我遇到了内存泄漏。下面给出的是最高输出。 最高输出: PID USER
我有一个应用程序实现了 boost named_mutex 以锁定 C++ 项目 (Visual Studio) 中的多个模块。我需要不惜一切代价删除所有 boost 依赖项。 还有其他方法可以实现吗
我似乎遇到了 boost::interprocess::file_lock 的问题 我的流程 1 本质上是 boost::interprocess::file_lock test_lock("
我是一名优秀的程序员,十分优秀!