在多个文件中全局运行boost:无锁队列
我想从这个网站上运行示例46.3的一个变体 . 我使用的是linux系统 我希望在头文件中定义队列q。我想有生产和消费功能在不同的文件。所以我想让global.h包含在多个文件中全局运行boost:无锁队列,boost,pthreads,lock-free,Boost,Pthreads,Lock Free,我想从这个网站上运行示例46.3的一个变体 . 我使用的是linux系统 我希望在头文件中定义队列q。我想有生产和消费功能在不同的文件。所以我想让global.h包含 static boost::lockfree::queue<int> q{100}; static std::atomic<int> sum{0}; void *produce (void*); void *consume (void*); 然后我想让main.cpp包含
static boost::lockfree::queue<int> q{100};
static std::atomic<int> sum{0};
void *produce (void*);
void *consume (void*);
然后我想让main.cpp包含
void *consume (void*)
{
int i;
while (q.pop(i))
sum += i;
}
#include iosteam
#include iomanip
#include global
#include pthread
int main ()
{pthread_t t1;
pthread_t t2;
pthread_t t3;
int t1_iret;
t1_iret = pthread_create( &t1, NULL, produce, NULL);
if(t1_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t1_iret);
exit(EXIT_FAILURE);
}
int t2_iret;
t2_iret = pthread_create( &t2, NULL, consume, NULL);
if(t2_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t2_iret);
exit(EXIT_FAILURE);
}
int t3_iret;
t3_iret = pthread_create( &t3, NULL, consume, NULL);
if(t3_iret)
{
fprintf(stderr,"Error - pthread_create() return code: %d\n",t3_iret);
exit(EXIT_FAILURE);
}
pthread_join( t1, NULL);
pthread_join( t2, NULL);
pthread_join( t3, NULL);
return 0; }
此外,我想知道是否可以用字符串而不是整数来实现我所描述的
edit1:当我尝试将队列设为字符串队列时,我得到::
/usr/local/include/boost/lockfree/queue.hpp:“类boost::l ockfree::queue>”的实例化中:
/home/ubuntu/Project/src/main.cpp:15:37:此处为必填项
/usr/local/include/boost/lockfree/queue.hpp:87:5:错误:静态断言失败:(boost::has_平凡_析构函数::value)
BOOST_STATIC_ASSERT((BOOST::has_triple_destructor::value));
^
/usr/local/include/boost/lockfree/queue.hpp:91:5:错误:静态断言失败:(boost::has_平凡_assign::value)
BOOST_STATIC_ASSERT((BOOST::has_平凡_assign::value));
^
在/usr/local/include/boost/lockfree/queue.hpp:21:0中包含的文件中,
从/home/ubuntu/Project/src/main.cpp:5:
/usr/local/include/boost/lockfree/detail/copy_payload.hpp:“静态void boost::lockfree::detail::copy_constructible_and_copyable::copy”的实例化中(T&,U&)[带T=std::basic_string;U=int]':
/usr/local/include/boost/lockfree/detail/copy_有效载荷。hpp:49:25:来自“void boost::lockfree::detail::copy_有效载荷”(T&,U&)的必填项[带T=std::basic_string;U=int]'
/usr/local/include/boost/lockfree/queue.hpp:402:61:必须来自'bool boost::lockfree::queue::pop(U&)[带U=int;T=std::basic_string;A0=boost::parameter::void;A1=boost::parameter::void;A2=boost::par ameter::void_U]'
/home/ubuntu/Project/src/main.cpp:21:24:从这里开始需要
/usr/local/include/boost/lockfree/detail/copy_payload.hpp:38:11:错误:从类型“std::basic_string”转换为类型“int”无效
u=u(t);您需要在
global.h
中声明而不是定义变量:
extern boost::lockfree::queue<int> q;
extern std::atomic<int> sum;
我认为这应该可以解决您的问题。有关详细信息,请参阅
至于第二部分,问你为什么不能创建一个字符串的无锁队列,这个问题由错误消息回答:
has\u trial\u destructor
对于std::string
,是错误的,因为它是一个分配内存的动态大小的字符串。你将无法在这种无锁队列中使用它。你可以尝试使用改为固定大小的字符串类,或std::array
您需要在global.h
中声明但不定义变量:
extern boost::lockfree::queue<int> q;
extern std::atomic<int> sum;
我认为这应该可以解决您的问题。有关详细信息,请参阅
至于第二部分,问你为什么不能创建一个字符串的无锁队列,这个问题由错误消息回答:
has\u trial\u destructor
对于std::string
,是错误的,因为它是一个分配内存的动态大小的字符串。你将无法在这种无锁队列中使用它。你可以尝试使用改为固定大小的字符串类,或std::array
好吧,我试过了,但我有两个问题,1)全局队列似乎没有在头文件中更新。2)此外,我不能创建字符串队列。@ArielBaron:好的,谢谢你提供的详细信息。我更新了我的答案以修复(1)。至于(2),你是什么意思“不能”?您收到错误消息了吗?对于2)我收到一个错误,对于1)总和显示为零,这表明队列未被激活filled@ArielBaron:“我犯了一个错误"没有用。具体是什么错误?粘贴整个文本。我将错误添加到问题中。谢谢你的帮助!我尝试了,但我遇到了两个问题,1)全局队列似乎没有在头文件中更新。2)此外,我无法创建字符串队列。@ArielBaron:好的,谢谢你提供详细信息。我更新了我的an回答修复(1)。至于(2),你说“不能”是什么意思?你收到错误消息了吗?对于2)我收到一个错误,对于1)总和显示为零,这表明队列没有被激活filled@ArielBaron:“我遇到错误”没有用。到底是什么错误?粘贴整个文本。我将错误添加到问题中。谢谢您的帮助!
boost::lockfree::queue<int> q{100};
std::atomic<int> sum{0};