- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试了几个小时,这个问题几乎让我发疯。
我想创建一个 spsc_queue
在共享内存上,队列中的每个元素都是一个 mq_item_t
下面的结构。
typedef struct _mq_item_t{
mq_item_type type;
union
{
struct{
log_level_t level;
char * text;
} log;
struct{
char * control;
size_t control_size;
char * payload;
size_t payload_size;
} error;
struct{
char * channel;
char * control;
size_t control_size;
char * payload;
size_t payload_size;
} data;
};
} mq_item_t;
spsc_queue
.
typedef boost::interprocess::managed_windows_shared_memory native_managed_shared_memory;
typedef boost::interprocess::allocator<mq_item_t, native_managed_shared_memory::segment_manager> shmem_allocator;
typedef boost::lockfree::spsc_queue< mq_item_t, boost::lockfree::allocator<shmem_allocator>> lockfree_queue;
m_segment = new native_managed_shared_memory(create_only, mem_name, SHARED_MEMORY_BYTES, NULL, perm);
shmem_allocator alloc(m_segment->get_segment_manager());
m_segment->find_or_construct<lockfree_queue>("name of the queue")(65535, alloc);
boost/lockfree/spsc_queue.hpp(609): error C2664: 'boost::lockfree::detail::ringbuffer_base<T>::pop' : cannot convert parameter 3 from 'boost::interprocess::offset_ptr<PointedType,DifferenceType,OffsetType,OffsetAlignment>' to 'mq_item_t *'
1> with
1> [
1> T=mq_item_t
1> ]
1> and
1> [
1> PointedType=mq_item_t,
1> DifferenceType=ptrdiff_t,
1> OffsetType=size_t,
1> OffsetAlignment=0x00
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> boost/lockfree/spsc_queue.hpp(608) : while compiling class template member function 'boost::lockfree::detail::runtime_sized_ringbuffer<T,Alloc>::size_type boost::lockfree::detail::runtime_sized_ringbuffer<T,Alloc>::pop(T *,boost::lockfree::detail::runtime_sized_ringbuffer<T,Alloc>::size_type)'
1> with
1> [
1> T=mq_item_t,
1> Alloc=boost::interprocess::allocator<mq_item_t,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,boost::interprocess::iset_index>>
1> ]
1> boost/lockfree/spsc_queue.hpp(681) : see reference to class template instantiation 'boost::lockfree::detail::runtime_sized_ringbuffer<T,Alloc>' being compiled
1> with
1> [
1> T=mq_item_t,
1> Alloc=boost::interprocess::allocator<mq_item_t,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,boost::interprocess::iset_index>>
1> ]
1> boost/interprocess/detail/named_proxy.hpp(213) : see reference to class template instantiation 'boost::lockfree::spsc_queue<T,A0>' being compiled
1> with
1> [
1> T=mq_item_t,
1> A0=boost::lockfree::allocator<shmem_allocator>
1> ]
1> boost/interprocess/detail/named_proxy.hpp(213) : while compiling class template member function 'void boost::interprocess::ipcdetail::CtorArg2<T,P0,P1>::construct_n(void *,size_t,size_t &)'
1> with
1> [
1> T=lockfree_queue,
1> P0=int,
1> P1=boost::interprocess::allocator<mq_item_t,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,boost::interprocess::iset_index>> &
1> ]
1> boost/interprocess/detail/named_proxy.hpp(282) : see reference to class template instantiation 'boost::interprocess::ipcdetail::CtorArg2<T,P0,P1>' being compiled
1> with
1> [
1> T=lockfree_queue,
1> P0=int,
1> P1=boost::interprocess::allocator<mq_item_t,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,boost::interprocess::iset_index>> &
1> ]
1> ..\common\sink.cpp(26) : see reference to function template instantiation 'T *boost::interprocess::ipcdetail::named_proxy<SegmentManager,T,is_iterator>::operator ()<int,shmem_allocator&>(P0 &&,P1) const' being compiled
1> with
1> [
1> T=lockfree_queue,
1> SegmentManager=boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,boost::interprocess::iset_index>,
1> is_iterator=false,
1> P0=int,
1> P1=shmem_allocator &
1> ]
最佳答案
我之前已经展示过如何做到这一点:Shared-memory IPC synchronization (lock-free)
我目前似乎无法制作 lockfree::allocator<>
版本工作(因为 spsc_queue
似乎不适用于涉及非原始指针( offset_ptr<>
))。
Strangely I couldn't make it work with older boost versions, so now I wonder whether that ever worked. But that's not really relevant:
boost::lockfree::capacity<65535>
而不是 boost::lockfree::allocator<>
.此 does compile ,但是 mq_item_t
到处都是原始指针。这永远不会起作用,因为指针在另一个进程空间中无效。我建议如下:typedef boost::interprocess::allocator<char, msm_t::segment_manager> char_alloc;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_alloc> shmem_string;
typedef boost::interprocess::vector<char, char_alloc> shmem_blob;
struct log {
typedef char_alloc allocator_type;
log(char_alloc alloc);
log(log_level_t level, const char* txt, char_alloc alloc);
log_level_t level;
shmem_string text;
};
struct error {
typedef char_alloc allocator_type;
error(char_alloc alloc);
template <typename Ctl, typename Payload>
error(Ctl const& ctl, Payload const& pl, char_alloc alloc);
shmem_blob control, payload;
};
struct data {
typedef char_alloc allocator_type;
data(char_alloc alloc);
template <typename Ctl, typename Payload>
data(char const* channel, Ctl const& ctl, Payload const& pl, char_alloc alloc);
shmem_string channel;
shmem_blob control, payload;
};
typedef boost::variant<log, error, data> item_t;
#include <boost/lockfree/spsc_queue.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/managed_mapped_file.hpp>
#include <boost/interprocess/containers/string.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/variant.hpp>
#include <boost/range.hpp>
#include <string>
#include <iostream>
namespace bip = boost::interprocess;
using boost::lockfree::spsc_queue;
#ifdef COLIRU
typedef bip::managed_mapped_file msm_t;
#else
typedef bip::managed_shared_memory msm_t;
#endif
namespace mq {
enum log_level_t { default_level };
typedef boost::interprocess::allocator<char, msm_t::segment_manager> char_alloc;
typedef boost::interprocess::basic_string<char, std::char_traits<char>, char_alloc> shmem_string;
typedef boost::interprocess::vector<char, char_alloc> shmem_blob;
using boost::begin;
using boost::end;
struct log {
typedef char_alloc allocator_type;
log(char_alloc alloc) : text(alloc) {}
log(log_level_t level, const char* txt, char_alloc alloc) : level(level), text(txt, alloc) { };
log_level_t level;
shmem_string text;
};
struct error {
typedef char_alloc allocator_type;
error(char_alloc alloc) : control(alloc), payload(alloc) {}
template <typename Ctl, typename Payload>
error(Ctl const& ctl, Payload const& pl, char_alloc alloc)
: control(begin(ctl), end(ctl), alloc),
payload(begin(pl), end(pl), alloc)
{ }
shmem_blob control, payload;
};
struct data {
typedef char_alloc allocator_type;
data(char_alloc alloc) : channel(alloc), control(alloc), payload(alloc) {}
template <typename Ctl, typename Payload>
data(char const* channel, Ctl const& ctl, Payload const& pl, char_alloc alloc)
: channel(channel, alloc),
control(begin(ctl), end(ctl), alloc),
payload(begin(pl), end(pl), alloc)
{ }
shmem_string channel;
shmem_blob control, payload;
};
typedef boost::variant<log, error, data> item_t;
}
namespace {
static char const* mem_name = "21845989-f5e0-4c84-b170-cd34dc9f37fc";
static constexpr size_t SHARED_MEMORY_BYTES = 10ul << 10;
}
int main()
{
typedef boost::interprocess::allocator<mq::item_t, msm_t::segment_manager> shmem_allocator;
#ifdef COLIRU
typedef boost::lockfree::spsc_queue<mq::item_t, boost::lockfree::capacity<10> > lockfree_queue;
#else
typedef boost::lockfree::spsc_queue<mq::item_t, boost::lockfree::capacity<65535> > lockfree_queue;
#endif
msm_t m_segment(bip::create_only, mem_name, SHARED_MEMORY_BYTES);
shmem_allocator alloc(m_segment.get_segment_manager());
auto queue = m_segment.find_or_construct<lockfree_queue>("name of the queue")();
queue->push(mq::error("control", "payload", alloc));
queue->push(mq::data("channel", "control", "payload", alloc));
queue->push(mq::log(mq::default_level, "text", alloc));
}
关于boost - 共享内存上的 spsc_queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30075646/
我试了几个小时,这个问题几乎让我发疯。 我想创建一个 spsc_queue在共享内存上,队列中的每个元素都是一个 mq_item_t下面的结构。 typedef struct _mq_item_t{
我正在包装 boost::lockfree::spsc_queue queue进入一个 RingBuffer 类,并希望能够在我的项目中使用这个 RingBuffer。但是我很难通过类构造函数将容量大
我正在尝试实现单个生产者(主线程)和单个消费者(从主线程生成的子线程)问题,因此根据我的搜索,我得到了 spsc_queue 作为最好的无锁数据结构由 boost 库提供。现在从他们的示例代码中获取,
我想使用运行时指定的最大条目数在共享内存中创建一个无锁环形缓冲区。我的代码基于我在 GitHub 中找到的示例.我使用这段代码在共享内存中成功创建了一个无锁环形缓冲区。 在我的例子中,我需要指定环形缓
我正在尝试 spsc_queue.pop() 这个结构 enum action_type { SUBSCRIBE, UNSUBSCRIBE, MESSAGE }; struct
我需要缓冲一堆传入的 10GigE 数据,以便稍后将其写入磁盘。按顺序执行此操作是一个问题,因为如果我不能足够快地维护它,我正在读取的设备将会溢出。 在寻找解决方案时,我偶然发现了 boost::lo
我这样声明 spsc_queue: boost::lockfree::spsc_queue> futInfoUpdates; 我是这样处理的: fut_sess_contents entry_fsc;
常规 std::vector 有 emplace_back 避免不必要的复制。 spsc_queue 不支持这个有什么原因吗?是否由于某种原因无法使用无锁队列emplace? 最佳答案 我既不是 bo
我在看 Boost's lockfree queue . 当生产者线程将数据结构 T 插入缓冲区时,它会(通过复制构造函数)将数据复制到缓冲区中。 当消费者线程尝试调用consume_one() 来读
我有一个线程在 boost::lockfree::spsc_queue 上生成数据,以及一个线程消耗数据。我可以让第三个线程使用 read_available 监视队列吗?或 write_availa
我有这样的队列: boost::lockfree::spsc_queue> futOrdersUpdates; 我的问题是有时我会多次使用同一个项目。我添加了一些故障排除代码: while (
以下是我的问题的表述。 #include class test { struct complicated { int x; int y; }; std::alloca
所以我正在使用 boost::lockfree::spec_queue 通过两个运行我应用程序中两个对象的仿函数的 boost_threads 进行通信。 除了 spec_queue::pop() 方
我正在尝试在 CUDA 中编写一个程序,该程序将在主机端使用 Boost 的单一生产者/单一消费者队列,但我一包含 就遇到了编译器错误。 . 此时,我没有在设备上执行任何操作,也没有创建 spsc_
如果我正在运行一个带有线程池的 boost::asio::io_service 并使用 boost::asio::strand 包装一个特定的套接字接收模拟单线程操作,有谁知道 strand 是否满足
我想直接从 boost 接收到 boost::lockfree:spsc_queue(或者,boost::circular_buffer)::asio::async_read 调用。看起来我需要编写一
我比较了这三种情况使用的运行时间,它们是boost::lockfree::queue, boost::lockfree::spsc_queue 以及使用std::queue的生产者/消费者队列的串行代
我定义了一个变量如下: boost::lockfree::spsc_queue, boost::lockfree::capacity > myVariable; 在我的代码中,我试图休息它: myVa
我是一名优秀的程序员,十分优秀!