微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 boost 的共享内存无锁环形缓冲区

如何解决使用 boost 的共享内存无锁环形缓冲区

任何人都可以帮助我通过正确的论点吗?我正在尝试遵循此处的答案:

Shared-memory IPC synchronization (lock-free)

或:

https://gist.github.com/19317362/29288bbbf6a7f031b2bbd5873267c919

我使用的代码是:producer.cpp

#include <boost/lockfree/spsc_queue.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>

namespace bip = boost::interprocess;
namespace shm
{
    typedef bip::allocator<char,bip::managed_shared_memory::segment_manager> char_alloc;
    typedef bip::basic_string<char,std::char_traits<char>,char_alloc >  shared_string;

    typedef boost::lockfree::spsc_queue<
        shared_string,boost::lockfree::capacity<1024> 
    > ring_buffer;
}

#include <unistd.h>

int main()
{
    // create segment and corresponding allocator
    bip::managed_shared_memory segment(bip::open_or_create,"MySharedMemory",65536);
    shm::char_alloc char_alloc(segment.get_segment_manager());

    // Ringbuffer fully constructed in shared memory. The element strings are
    // also allocated from the same shared memory segment. This vector can be
    // safely accessed from other processes.
    shm::ring_buffer *queue = segment.find_or_construct<shm::ring_buffer>("queue")();

    const char* messages[] = { "hello world","the answer is 42","where is your towel",0 };

    for (const char** msg_it = messages; *msg_it; ++msg_it)
    {
        sleep(1);
        queue->push(shm::shared_string(*msg_it,char_alloc));
    }
}

和消费者.cpp

#include <boost/lockfree/spsc_queue.hpp> // ring buffer
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <boost/interprocess/containers/string.hpp>

namespace bip = boost::interprocess;
namespace shm
{
    typedef bip::allocator<char,char_alloc >      shared_string;

    typedef boost::lockfree::spsc_queue<
        shared_string,boost::lockfree::capacity<1024> 
    > ring_buffer;
}

#include <iostream>

int main()
{
    // create segment and corresponding allocator
    bip::managed_shared_memory segment(bip::open_or_create,65536);
    shm::char_alloc char_alloc(segment.get_segment_manager());

    shm::ring_buffer *queue = segment.find_or_construct<shm::ring_buffer>("queue")();

    while (true)
    {
        shm::shared_string v(char_alloc);
        if (queue->pop(v))
            std::cout << "Processed: '" << v << "'\n";
    }
}

然而,当我尝试从命令行编译时,我得到以下信息:

$ g++ consumer.cpp -o consumer
In file included from /usr/include/boost/interprocess/containers/string.hpp:19:0,from consumer.cpp:4:
/usr/include/boost/container/string.hpp: In instantiation of ‘boost::container::container_detail::basic_string_base<Allocator>::members_holder::members_holder() [with Allocator = boost::interprocess::allocator<char,boost::interprocess::segment_manager<char,boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>,boost::interprocess::iset_index> >]’:
/usr/include/boost/container/string.hpp:104:18:   required from ‘boost::container::container_detail::basic_string_base<Allocator>::basic_string_base() [with Allocator = boost::interprocess::allocator<char,boost::interprocess::iset_index> >]’
/usr/include/boost/container/string.hpp:596:16:   required from ‘boost::container::basic_string<CharT,Traits,Allocator>::basic_string() [with CharT = char; Traits = std::char_traits<char>; Allocator = boost::interprocess::allocator<char,boost::interprocess::iset_index> >]’
/usr/include/boost/array.hpp:60:11:   required from ‘boost::lockfree::spsc_queue<T,A0,A1>::spsc_queue() [with T = boost::container::basic_string<char,boost::interprocess::allocator<char,boost::interprocess::iset_index> > >; A0 = boost::lockfree::capacity<1024ul>; A1 = boost::parameter::void_]’
/usr/include/boost/interprocess/detail/named_proxy.hpp:155:10:   required from ‘void boost::interprocess::ipcdetail::Ctor0Arg<T>::construct_n(void*,std::size_t,std::size_t&) [with T = boost::lockfree::spsc_queue<boost::container::basic_string<char,boost::interprocess::iset_index> > >,boost::lockfree::capacity<1024ul> >; std::size_t = long unsigned int]’
consumer.cpp:34:1:   required from here
/usr/include/boost/container/string.hpp:218:22: error: no matching function for call to ‘boost::interprocess::allocator<char,boost::interprocess::iset_index> >::allocator()’
          : Allocator()
                      ^
/usr/include/boost/container/string.hpp:218:22: note: candidates are:
In file included from /usr/include/boost/interprocess/segment_manager.hpp:33:0,from /usr/include/boost/interprocess/detail/managed_memory_impl.hpp:26,from /usr/include/boost/interprocess/managed_shared_memory.hpp:21,from consumer.cpp:2:
/usr/include/boost/interprocess/allocators/allocator.hpp:140:4: note: template<class T2> boost::interprocess::allocator<T,SegmentManager>::allocator(const boost::interprocess::allocator<T2,SegmentManager>&)
    allocator(const allocator<T2,SegmentManager> &other)
    ^
/usr/include/boost/interprocess/allocators/allocator.hpp:140:4: note:   template argument deduction/substitution Failed:
In file included from /usr/include/boost/interprocess/containers/string.hpp:19:0,from consumer.cpp:4:
/usr/include/boost/container/string.hpp:218:22: note:   candidate expects 1 argument,0 provided
          : Allocator()
                      ^
In file included from /usr/include/boost/interprocess/segment_manager.hpp:33:0,from consumer.cpp:2:
/usr/include/boost/interprocess/allocators/allocator.hpp:134:4: note: boost::interprocess::allocator<T,SegmentManager>::allocator(const boost::interprocess::allocator<T,SegmentManager>&) [with T = char; SegmentManager = boost::interprocess::segment_manager<char,boost::interprocess::iset_index>]
    allocator(const allocator &other)
    ^
/usr/include/boost/interprocess/allocators/allocator.hpp:134:4: note:   candidate expects 1 argument,0 provided
/usr/include/boost/interprocess/allocators/allocator.hpp:129:4: note: boost::interprocess::allocator<T,SegmentManager>::allocator(boost::interprocess::allocator<T,SegmentManager>::segment_manager*) [with T = char; SegmentManager = boost::interprocess::segment_manager<char,boost::interprocess::iset_index>; boost::interprocess::allocator<T,SegmentManager>::segment_manager = boost::interprocess::segment_manager<char,boost::interprocess::iset_index>]
    allocator(segment_manager *segment_mngr)
    ^
/usr/include/boost/interprocess/allocators/allocator.hpp:129:4: note:   candidate expects 1 argument,0 provided

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?