5

我正在尝试在此文件中boost::lockfree::queue替换https://github.com/zaphoyd/websocketpp/blob/experimental/examples/broadcast_server/broadcast_server.cppstd::queue

我添加了#include <boost/lockfree/queue.hpp>;将行130, std::queue<action> m_actions;, 更改为boost::lockfree::queue<action> m_actions;; 删除了所有与锁定有关的行;并将行103, m_actions.pop();, 更改为m_actions.pop(a);.

在添加到项目根目录并在目录中使用's后,我scons broadcast_server_lockfree在项目根目录中遇到这些错误:broadcast_server_lockfree = SConscript('#/broadcast_server_lockfree/SConscript',variant_dir = builddir + 'broadcast_server_lockfree',duplicate = 0)SConstructbroadcast_serverSConstructbroadcast_server_lockfree

root@server:~/websocketpp-experimental# scons broadcast_server_lockfree
scons: Reading SConscript files ...
C++11 build environment partially enabled
scons: done reading SConscript files.
scons: Building targets ...
scons: building associated VariantDir targets: build/release/broadcast_server_lockfree
g++ -o build/release/broadcast_server_lockfree/broadcast_server_lockfree.o -c -std=c++0x -Wall -Wcast-align -isystem /root/boost_1_53_0 -DNDEBUG -D_WEBSOCKETPP_CPP11_MEMORY_ -D_WEBSOCKETPP_CPP11_FUNCTIONAL_ -D_WEBSOCKETPP_CPP11_SYSTEM_ERROR_ -D_WEBSOCKETPP_CPP11_RANDOM_DEVICE_ -D_WEBSOCKETPP_NOEXCEPT_ -I. broadcast_server_lockfree/broadcast_server_lockfree.cpp
In file included from broadcast_server_lockfree/broadcast_server_lockfree.cpp:10:0:
/root/boost_1_53_0/boost/lockfree/queue.hpp: In instantiation of 'class boost::lockfree::queue<action>':
broadcast_server_lockfree/broadcast_server_lockfree.cpp:139:36:   required from here
/root/boost_1_53_0/boost/lockfree/queue.hpp:79:5: error: static assertion failed: (boost::has_trivial_destructor<T>::value)
/root/boost_1_53_0/boost/lockfree/queue.hpp:83:5: error: static assertion failed: (boost::has_trivial_assign<T>::value)
broadcast_server_lockfree/broadcast_server_lockfree.cpp: In member function 'void broadcast_server::process_messages()':
broadcast_server_lockfree/broadcast_server_lockfree.cpp:111:34: error: 'class boost::lockfree::queue<action>' has no member named 'front'
broadcast_server_lockfree/broadcast_server_lockfree.cpp:117:55: error: 'm_connection_lock' was not declared in this scope
broadcast_server_lockfree/broadcast_server_lockfree.cpp:120:55: error: 'm_connection_lock' was not declared in this scope
broadcast_server_lockfree/broadcast_server_lockfree.cpp:123:55: error: 'm_connection_lock' was not declared in this scope
In file included from broadcast_server_lockfree/broadcast_server_lockfree.cpp:10:0:
/root/boost_1_53_0/boost/lockfree/queue.hpp: In instantiation of 'boost::lockfree::queue<T, A0, A1, A2>::~queue() [with T = action; A0 = boost::parameter::void_; A1 = boost::parameter::void_; A2 = boost::parameter::void_]':
broadcast_server_lockfree/broadcast_server_lockfree.cpp:41:24:   required from here
/root/boost_1_53_0/boost/lockfree/queue.hpp:229:11: error: no matching function for call to 'action::action()'
/root/boost_1_53_0/boost/lockfree/queue.hpp:229:11: note: candidates are:
broadcast_server_lockfree/broadcast_server_lockfree.cpp:32:5: note: action::action(action_type, websocketpp::endpoint<websocketpp::connection<websocketpp::config::asio>, websocketpp::config::asio>::message_ptr)
broadcast_server_lockfree/broadcast_server_lockfree.cpp:32:5: note:   candidate expects 2 arguments, 0 provided
broadcast_server_lockfree/broadcast_server_lockfree.cpp:31:5: note: action::action(action_type, websocketpp::connection_hdl)
broadcast_server_lockfree/broadcast_server_lockfree.cpp:31:5: note:   candidate expects 2 arguments, 0 provided
broadcast_server_lockfree/broadcast_server_lockfree.cpp:30:8: note: action::action(const action&)
broadcast_server_lockfree/broadcast_server_lockfree.cpp:30:8: note:   candidate expects 1 argument, 0 provided
In file included from broadcast_server_lockfree/broadcast_server_lockfree.cpp:10:0:
/root/boost_1_53_0/boost/lockfree/queue.hpp: In instantiation of 'boost::lockfree::queue<T, A0, A1, A2>::node::node(boost::lockfree::queue<T, A0, A1, A2>::node::handle_type) [with T = action; A0 = boost::parameter::void_; A1 = boost::parameter::void_; A2 = boost::parameter::void_; boost::lockfree::queue<T, A0, A1, A2>::node::handle_type = boost::lockfree::queue<action>::node*]':
/root/boost_1_53_0/boost/lockfree/detail/freelist.hpp:82:13:   required from 'T* boost::lockfree::detail::freelist_stack<T, Alloc>::construct(const ArgumentType&) [with bool ThreadSafe = true; bool Bounded = false; ArgumentType = boost::lockfree::queue<action>::node*; T = boost::lockfree::queue<action>::node; Alloc = std::allocator<boost::lockfree::queue<action>::node>]'
/root/boost_1_53_0/boost/lockfree/queue.hpp:126:75:   required from 'void boost::lockfree::queue<T, A0, A1, A2>::initialize() [with T = action; A0 = boost::parameter::void_; A1 = boost::parameter::void_; A2 = boost::parameter::void_]'
/root/boost_1_53_0/boost/lockfree/queue.hpp:166:9:   required from 'boost::lockfree::queue<T, A0, A1, A2>::queue() [with T = action; A0 = boost::parameter::void_; A1 = boost::parameter::void_; A2 = boost::parameter::void_]'
broadcast_server_lockfree/broadcast_server_lockfree.cpp:41:24:   required from here
/root/boost_1_53_0/boost/lockfree/queue.hpp:109:52: error: no matching function for call to 'action::action()'
/root/boost_1_53_0/boost/lockfree/queue.hpp:109:52: note: candidates are:
broadcast_server_lockfree/broadcast_server_lockfree.cpp:32:5: note: action::action(action_type, websocketpp::endpoint<websocketpp::connection<websocketpp::config::asio>, websocketpp::config::asio>::message_ptr)
broadcast_server_lockfree/broadcast_server_lockfree.cpp:32:5: note:   candidate expects 2 arguments, 0 provided
broadcast_server_lockfree/broadcast_server_lockfree.cpp:31:5: note: action::action(action_type, websocketpp::connection_hdl)
broadcast_server_lockfree/broadcast_server_lockfree.cpp:31:5: note:   candidate expects 2 arguments, 0 provided
broadcast_server_lockfree/broadcast_server_lockfree.cpp:30:8: note: action::action(const action&)
broadcast_server_lockfree/broadcast_server_lockfree.cpp:30:8: note:   candidate expects 1 argument, 0 provided
scons: *** [build/release/broadcast_server_lockfree/broadcast_server_lockfree.o] Error 1
scons: building terminated because of errors.

我对 C++ 几乎一无所知,并且对错误的搜索没有产生任何结果(因为我不知道我在读什么)。

boost::lockfree::queue如果有帮助, 这是示例。http://boost-sandbox.sourceforge.net/doc/html/lockfree/examples.html

请告诉我如何纠正这个问题。

action

struct action {
    action(action_type t, connection_hdl h) : type(t), hdl(h) {}
    action(action_type t, server::message_ptr m) : type(t), msg(m) {}

    action_type type;
    websocketpp::connection_hdl hdl;
    server::message_ptr msg;
};
4

3 回答 3

9

文档

T 必须有一个复制构造函数、一个普通赋值运算符和一个普通析构函数

因此,您的action课程必须如下所示:

class action
{
public:
    action(const action& rhs) { ... }
    //Implicitly defined destructor for itself and all member variables
    //Implicitly defined operator= for itself and all member variables

};

s 抱怨是因为你的static_assert析构函数并operator=没有被编译器隐式定义(或者这是至少一个成员变量的情况action)。

编辑:我已经快速浏览了 repo - 我似乎找不到connection_hdl,并且没有message_ptrin template <typename endpoint> class server。无论哪种方式,满足websocketpp::connection_hdl hdlserver::message_ptr msg不满足上述条件之一。仅仅“放入”一个无锁队列而不是使用 astd::queue可能是一项不平凡的任务,需要进行许多更改。

于 2013-03-18T01:13:45.687 回答
1

上面的帖子已经很好地回答了这个问题。在这里,我将提供更多示例。富有成果的讨论提供了很多指导,请阅读它们。真正的问题是对队列模板类型的要求。为了实用,让我们看一些真实的用例。

namespace lkf=boost::lockfree;
vector<lkf::spsc_queue<pair<Fastq*,int>, lkf::capacity<100>>> datastore;
//lkf::queue<pair<char*,char*>, lkf::capacity<100>> pile1, pile2;  //failed assert
lkf::queue<pair<char*,char*>*, lkf::capacity<100>> pile1, pile2;

代码中的第一行通过了编译器(因为它是 spac_queue),但第二行对编译器失败了(由于 boost::ASSERT):

/usr/local/include/boost/lockfree/queue.hpp:99:5: error: static assertion failed: (boost::has_trivial_assign<T>::value)
     BOOST_STATIC_ASSERT((boost::has_trivial_assign<T>::value));

将 pair 转换为指针类型(第 3 行)后,编译器很高兴。

可能需要更新提升断言吗?转换为指针是一种快速技巧,可能不是最好的。

boost lockfree::queue 有三个要求

  1. T 必须有一个复制构造函数
  2. T 必须有一个平凡的赋值运算符
  3. T 必须有一个简单的析构函数

spsc_queue 有两个:

  1. T 必须有一个默认构造函数
  2. T 必须是可复制的
于 2019-03-29T21:04:09.657 回答
0

std::string用于队列项目类型时出现此错误。

boost::lockfree::queue<string>(10000)

正如许多好人在这里这里的答案中指出的那样,不允许使用字符串boost::lockfree::queue

它确实编译char *

boost::lockfree::queue<char*>(10000)

于 2020-11-24T10:36:38.370 回答