- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用运行时指定的最大条目数在共享内存中创建一个无锁环形缓冲区。我的代码基于我在 GitHub 中找到的示例.我使用这段代码在共享内存中成功创建了一个无锁环形缓冲区。
在我的例子中,我需要指定环形缓冲区在运行时构造时可以接受的最大条目数,而不是每个示例的编译时。构建 shm::ring_buffer
的调用在示例中如下所示。
namespace bip = boost::interprocess;
namespace shm
{
using char_alloc = bip::allocator<char, bip::managed_shared_memory::segment_manager>;
using shared_string = bip::basic_string<char, std::char_traits<char>, char_alloc>;
using ring_buffer = boost::lockfree::spsc_queue<shared_string, boost::lockfree::capacity<200>>;
}
共享内存段分配如下:
mQueuingSharedMem = std::make_unique<bip::managed_shared_memory>(
bip::open_or_create, (mSharedMemoryName + "Queuing").c_str(), rSHMSize);
根据 GitHub 示例,当我通过可选的 boost::lockfree::capacity<>
构造具有在编译时指定的最大大小的环形缓冲区时模板参数,一切正常(注意:共享内存段 construct
方法采用 [] 中的#ring_buffers,构造函数参数在后面的括号中指定。
auto pSharedMemAddr = mQueuingSharedMem->construct<
shm::ring_buffer>(rQueuingPortName.c_str())[1](/*aMaxNumMessages*/);
我认为为了在运行时构造上面的 shm::ring_buffer,我需要删除第二个 boost::lockfree::capacity<200>
来自 shm::spsc_queue
的硬编码大小参数而是传递 shm::ring_buffer
的最大尺寸和 shm::shm_string
的共享内存分配器.我找到了类似的答案 here但我无法使其适应我的代码。
我对上面运行的代码进行了以下更改,试图在运行时指定环形缓冲区的大小:
namespace bip = boost::interprocess;
namespace shm
{
using char_alloc = bip::allocator<char, bip::managed_shared_memory::segment_manager>;
using shared_string = bip::basic_string<char, std::char_traits<char>, char_alloc>;
using ring_buffer = boost::lockfree::spsc_queue<shared_string/*, boost::lockfree::capacity<200>*/>;
}
shm::char_alloc char_alloc(mQueuingSharedMem->get_segment_manager());
auto pSharedMemAddr = mQueuingSharedMem->construct<
shm::ring_buffer>(rQueuingPortName.c_str())[1](aMaxNumMessages);
我收到大量无法理解的编译器错误,我不太清楚如何修复:
1>------ Build started: Project: apex, Configuration: Debug x64 ------
1>APEXManager.cpp
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/container/string.hpp(216): error C2512: 'boost::interprocess::allocator<char,boost::interprocess::segment_manager<CharType,MemoryAlgorithm,IndexType>>::allocator': no appropriate default constructor available
1> with
1> [
1> CharType=char,
1> MemoryAlgorithm=boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void,ptrdiff_t,uintptr_t,0>,0>,
1> IndexType=boost::interprocess::iset_index
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/container/string.hpp(214): note: while compiling class template member function 'boost::container::container_detail::basic_string_base<Allocator>::members_holder::members_holder(void)'
1> with
1> [
1> Allocator=shm::char_alloc
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/container/string.hpp(100): note: see reference to function template instantiation 'boost::container::container_detail::basic_string_base<Allocator>::members_holder::members_holder(void)' being compiled
1> with
1> [
1> Allocator=shm::char_alloc
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/container/string.hpp(224): note: see reference to class template instantiation 'boost::container::container_detail::basic_string_base<Allocator>::members_holder' being compiled
1> with
1> [
1> Allocator=shm::char_alloc
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/container/string.hpp(506): note: see reference to class template instantiation 'boost::container::container_detail::basic_string_base<Allocator>' being compiled
1> with
1> [
1> Allocator=shm::char_alloc
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/lockfree/spsc_queue.hpp(557): note: see reference to class template instantiation 'boost::container::basic_string<char,std::char_traits<char>,shm::char_alloc>' being compiled
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/lockfree/spsc_queue.hpp(555): note: while compiling class template member function 'boost::lockfree::detail::runtime_sized_ringbuffer<T,std::allocator<T>>::~runtime_sized_ringbuffer(void)'
1> with
1> [
1> T=shm::shared_string
1> ]
1>..\..\src\apex\APEXManager.cpp(660): note: see reference to function template instantiation 'boost::lockfree::detail::runtime_sized_ringbuffer<T,std::allocator<T>>::~runtime_sized_ringbuffer(void)' being compiled
1> with
1> [
1> T=shm::shared_string
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/lockfree/spsc_queue.hpp(693): note: see reference to class template instantiation 'boost::lockfree::detail::runtime_sized_ringbuffer<T,std::allocator<T>>' being compiled
1> with
1> [
1> T=shm::shared_string
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/interprocess/detail/in_place_interface.hpp(61): note: see reference to class template instantiation 'boost::lockfree::spsc_queue<shm::shared_string>' being compiled
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/interprocess/detail/in_place_interface.hpp(58): note: while compiling class template member function 'void boost::interprocess::ipcdetail::placement_destroy<T>::destroy_n(void *,::size_t,size_t &)'
1> with
1> [
1> T=shm::ring_buffer
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/interprocess/detail/named_proxy.hpp(50): note: see reference to class template instantiation 'boost::interprocess::ipcdetail::placement_destroy<T>' being compiled
1> with
1> [
1> T=shm::ring_buffer
1> ]
1>C:\Users\johnc\main\extlibs\boost_1_65_1\boost/interprocess/detail/named_proxy.hpp(130): note: see reference to class template instantiation 'boost::interprocess::ipcdetail::CtorArgN<T,false,const MESSAGE_RANGE_TYPE &>' being compiled
1> with
1> [
1> T=shm::ring_buffer
1> ]
1>..\..\src\apex\APEXManager.cpp(365): note: see reference to function template instantiation 'T *boost::interprocess::ipcdetail::named_proxy<boost::interprocess::segment_manager<CharType,MemoryAlgorithm,IndexType>,T,false>::operator ()<const MESSAGE_RANGE_TYPE&>(const MESSAGE_RANGE_TYPE &) const' being compiled
1> with
1> [
1> T=shm::ring_buffer,
1> CharType=char,
1> MemoryAlgorithm=boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void,ptrdiff_t,uintptr_t,0>,0>,
1> IndexType=boost::interprocess::iset_index
1> ]
1>..\..\src\apex\APEXManager.cpp(365): note: see reference to function template instantiation 'T *boost::interprocess::ipcdetail::named_proxy<boost::interprocess::segment_manager<CharType,MemoryAlgorithm,IndexType>,T,false>::operator ()<const MESSAGE_RANGE_TYPE&>(const MESSAGE_RANGE_TYPE &) const' being compiled
1> with
1> [
1> T=shm::ring_buffer,
1> CharType=char,
1> MemoryAlgorithm=boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family,boost::interprocess::offset_ptr<void,ptrdiff_t,uintptr_t,0>,0>,
1> IndexType=boost::interprocess::iset_index
1> ]
1>Done building project "apex.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========
最佳答案
动态大小仅在您指定进程间内存分配器时才有效:http://www.boost.org/doc/libs/1_65_1/doc/html/boost/lockfree/allocator.html .
可悲的是spsc_queue
does support状态分配器:
Defines the allocator. boost.lockfree supports stateful allocator and is compatible with Boost.Interprocess allocators
它似乎不支持 uses_allocator<>
将分配器向下传递到其元素 ( shared_string
) 所需的协议(protocol),即使使用 scoped_allocator_adaptor
也不需要¹.
我之前遇到过这个:
Shared-memory IPC synchronization (lock-free) (使用编译时大小 spsc_queue
)
所以我的建议是去除其中一种成分:
对于后者,您可以使队列存储成为受管理的 bip::shared_ptr<shared_string>
相反:
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp> // for Coliru
#include <boost/interprocess/smart_ptr/shared_ptr.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <iostream>
/// noisy - traces special members
struct noisy {
noisy& operator=(noisy&&) noexcept { std::cout << "operator=(noisy&&)\n"; return *this; }
noisy& operator=(const noisy&) { std::cout << "operator=(const noisy&)\n"; return *this; }
noisy(const noisy&) { std::cout << "noisy(const noisy&)\n"; }
noisy(noisy&&) noexcept { std::cout << "noisy(noisy&&)\n"; }
~noisy() { std::cout << "~noisy()\n"; }
noisy() { std::cout << "noisy()\n"; }
};
namespace bip = boost::interprocess;
namespace blf = boost::lockfree;
namespace Shared {
using Segment = bip::managed_mapped_file; // Coliru unsupported: managed_shared_memory;
using Manager = Segment::segment_manager;
template <typename T> using Alloc = bip::allocator<T, Manager>;
using String = bip::basic_string<char, std::char_traits<char>, Alloc<char> >;
// using Element = String;
// For debug/demonstration
struct Element : String, noisy { using String::String; }; // inherit constructors
using Ptr = bip::managed_shared_ptr<Element, Segment>::type;
using Buffer = blf::spsc_queue<Ptr, blf::allocator<Alloc<Ptr> > >;
}
static std::string unique_id_gen() {
static std::atomic_size_t s_gen { 0 };
return "buffer_element" + std::to_string(++s_gen);
}
int main() {
struct shm_remove {
shm_remove() { bip::shared_memory_object::remove("MySharedMemory"); }
~shm_remove() { bip::shared_memory_object::remove("MySharedMemory"); }
} remover;
Shared::Segment segment(bip::create_only, "MySharedMemory", 4 << 20);
auto& buffer = *segment.construct<Shared::Buffer>(bip::unique_instance)[1](20, segment.get_segment_manager());
auto create = [&segment](auto&&... args) {
return make_managed_shared_ptr(segment.construct<Shared::Element>(unique_id_gen().c_str())
(
std::forward<decltype(args)>(args)...,
segment.get_segment_manager()
), segment);
};
std::cout << "Pushing\n";
for (auto msg : { "hello", "world", "bye", "cruel", "world" })
buffer.push(create(msg));
std::cout << "Popping\n";
{
Shared::Ptr into;
while (buffer.pop(into)) {
std::cout << "Popped: '" << *into << "'\n";
}
std::cout << "Going out of scope\n";
} // RAII
std::cout << "Out of scope\n";
{
// make sure any other owned queue elements are freed if the queue is destroyed before it's empty:
for (auto msg : { "HELLO", "WORLD", "BYE", "CRUEL", "WORLD" })
buffer.push(create(msg));
std::cout << "Destroying buffer containing 5 elements\n";
segment.destroy<Shared::Buffer>(bip::unique_instance);
}
}
打印:
Pushing
noisy()
noisy()
noisy()
noisy()
noisy()
Popping
Popped: 'hello'
~noisy()
Popped: 'world'
~noisy()
Popped: 'bye'
~noisy()
Popped: 'cruel'
~noisy()
Popped: 'world'
Going out of scope
~noisy()
Out of scope
noisy()
noisy()
noisy()
noisy()
noisy()
Destroying buffer containing 5 elements
~noisy()
~noisy()
~noisy()
~noisy()
~noisy()
¹ 搜索我的答案,了解如何将这些容器与共享内存中的其他容器一起使用
关于c++ - 无法在共享内存中使用运行时大小参数构造运行时 boost spsc_queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47026590/
简而言之:我想从可变参数模板参数中提取各种选项,但不仅通过标签而且通过那些参数的索引,这些参数是未知的 标签。我喜欢 boost 中的方法(例如 heap 或 lockfree 策略),但想让它与 S
我可以对单元格中的 excel IF 语句提供一些帮助吗? 它在做什么? 对“BaselineAmount”进行了哪些评估? =IF(BaselineAmount, (Variance/Baselin
我正在使用以下方法: public async Task Save(Foo foo,out int param) { ....... MySqlParameter prmparamID
我正在使用 CodeGear RAD Studio IDE。 为了使用命令行参数测试我的应用程序,我多次使用了“运行 -> 参数”菜单中的“参数”字段。 但是每次我给它提供一个新值时,它都无法从“下拉
我已经为信用卡类编写了一些代码,粘贴在下面。我有一个接受上述变量的构造函数,并且正在研究一些方法将这些变量格式化为字符串,以便最终输出将类似于 号码:1234 5678 9012 3456 截止日期:
MySql IN 参数 - 在存储过程中使用时,VarChar IN 参数 val 是否需要单引号? 我已经像平常一样创建了经典 ASP 代码,但我没有更新该列。 我需要引用 VarChar 参数吗?
给出了下面的开始,但似乎不知道如何完成它。本质上,如果我调用 myTest([one, Two, Three], 2); 它应该返回元素 third。必须使用for循环来找到我的解决方案。 funct
将 1113355579999 作为参数传递时,该值在函数内部变为 959050335。 调用(main.c): printf("%d\n", FindCommonDigit(111335557999
这个问题在这里已经有了答案: Is Java "pass-by-reference" or "pass-by-value"? (92 个回答) 关闭9年前。 public class StackOve
我真的很困惑,当像 1 == scanf("%lg", &entry) 交换为 scanf("%lg", &entry) == 1 没有区别。我的实验书上说的是前者,而我觉得后者是可以理解的。 1 =
我正在尝试使用调用 SetupDiGetDeviceRegistryProperty 的函数使用德尔福 7。该调用来自示例函数 SetupEnumAvailableComPorts .它看起来像这样:
我需要在现有项目上实现一些事件的显示。我无法更改数据库结构。 在我的 Controller 中,我(从 ajax 请求)传递了一个时间戳,并且我需要显示之前的 8 个事件。因此,如果时间戳是(转换后)
rails 新手。按照多态关联的教程,我遇到了这个以在create 和destroy 中设置@client。 @client = Client.find(params[:client_id] || p
通过将 VM 参数设置为 -Xmx1024m,我能够通过 Eclipse 运行 Java 程序-Xms256M。现在我想通过 Windows 中的 .bat 文件运行相同的 Java 程序 (jar)
我有一个 Delphi DLL,它在被 Delphi 应用程序调用时工作并导出声明为的方法: Procedure ProduceOutput(request,inputs:widestring; va
浏览完文档和示例后,我还没有弄清楚 schema.yaml 文件中的参数到底用在哪里。 在此处使用 AWS 代码示例:https://github.com/aws-samples/aws-proton
程序参数: procedure get_user_profile ( i_attuid in ras_user.attuid%type, i_data_group in data_g
我有一个字符串作为参数传递给我的存储过程。 dim AgentString as String = " 'test1', 'test2', 'test3' " 我想在 IN 中使用该参数声明。 AND
这个问题已经有答案了: When should I use "this" in a class? (17 个回答) 已关闭 6 年前。 我运行了一些java代码,我看到了一些我不太明白的东西。为什么下
我输入 scroll(0,10,200,10);但是当它运行时,它会传递字符串“xxpos”或“yypos”,我确实在没有撇号的情况下尝试过,但它就是行不通。 scroll = function(xp
我是一名优秀的程序员,十分优秀!