我正在尝试实现跨多个线程的消息传递系统。 boost::lockfree::queue
似乎是一个很好的方向,不幸的是我无法创建std::function
或boost::function
类型的队列,因为显然他们没有简单的任务和析构函数,这是要求boost::lockfree::queue
。
我的以下代码:
#include <cassert>
//#include <functional>
#include <iostream>
#include <boost/function.hpp>
#include <boost/lockfree/queue.hpp>
int main()
{
boost::lockfree::queue<boost::function<void(int)>> queue;
assert(queue.is_lock_free());
for(int j = 0; j < 50; ++j) {
queue.push([] (int i) { std::cout << i << std::endl; });
}
int i = 0;
boost::function<void(int)> functor;
while (queue.pop(functor)) {
functor(i++);
}
}
产生以下输出:
In file included from /usr/include/boost/integer.hpp:23:0,
from /usr/include/boost/function/function_base.hpp:21,
from /usr/include/boost/function/detail/prologue.hpp:17,
from /usr/include/boost/function.hpp:24,
from lockfree.cpp:5:
/usr/include/boost/lockfree/queue.hpp: In instantiation of ‘class boost::lockfree::queue<boost::function<void(int)> >’:
lockfree.cpp:10:54: required from here
/usr/include/boost/lockfree/queue.hpp:81:5: error: static assertion failed (boost::has_trivial_destructor<T>::value)
BOOST_STATIC_ASSERT((boost::has_trivial_destructor<T>::value));
^
/usr/include/boost/lockfree/queue.hpp:85:5: error: static assertion failed (boost::has_trivial_assign<T>::value)
BOOST_STATIC_ASSERT((boost::has_trivial_assign<T>::value));
有没有办法让这项工作?
答案 0 :(得分:5)
如果这是一个选项,您仍然可以将无状态 lambdas与原始函数指针一起使用:
#include <cassert>
#include <functional>
#include <iostream>
#include <boost/function.hpp>
#include <boost/lockfree/queue.hpp>
int main() {
//boost::lockfree::queue<boost::function<void(int)>> queue(50);
boost::lockfree::queue<void (*)(int)> queue(50);
assert(queue.is_lock_free());
for (int j = 0; j < 50; ++j) {
queue.push([](int i) { std::cout << i << std::endl; });
}
int i = 0;
boost::function<void(int)> functor;
while (queue.pop(functor)) {
functor(i++);
}
}
答案 1 :(得分:3)
您可以使用指针来模拟普通类型。 (这适用于所有lambdas)
#include <cassert>
#include <functional>
#include <iostream>
#include <boost/function.hpp>
#include <boost/lockfree/queue.hpp>
int main()
{
boost::lockfree::queue<boost::function<void(int)>*> queue(3);
assert(queue.is_lock_free());
for(int j = 0; j < 50; ++j) {
auto function = [] (int i) { std::cout << i << std::endl; };
queue.push(new boost::function<void(int)>(function));
}
int i = 0;
boost::function<void(int)> * functor;
while (queue.pop(functor)) {
functor->operator()(i++);
delete functor;
}
}
答案 2 :(得分:1)