升压SPSC队列段错误 [英] Boost spsc queue segfault

查看:180
本文介绍了升压SPSC队列段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用的boost :: lockfree :: spsc_queue 。 Occasinally找到段错误,与的std ::矢量&lt用它; uint16_t> 。整个类型是:

  spsc_queue<矢量< uint16_t>中的boost :: lockfree ::能力和LT; 1000 GT;>队列;

调试表明,该段错误happend在弹出,这是举办这样的:

 矢量<&uint16_t GT; VEC;
而(!完成)
  而(quuee.pop(安培; VEC)){/ *做的工作人员* /}

在spsc_queue

文档


  

BOOL 弹出(T&安培; RET);


  
  

从ringbuffer中弹出一个对象。


但是,当我使用

 矢量<&uint16_t GT; VEC;
而(!完成)
  而(quuee.pop(安培; VEC,1)){
    / *做的工作人员* /
  }

段错误奇迹般地消失了。
这使我想到,那布尔弹出(T&安培;)莫名其妙地尝试恢复尽可能多的项目成为可能。

我是不是正确使用这个容器?难道这个问题应该发生在纯矢量状容器?是员工很少使用,所以它是越野车?当我使用它与向量我可能会面临哪些注意事项?


解决方案

  

段错误奇迹般地消失了。这使我想到,那布尔流行(T&安培;)以某种方式试图恢复尽可能多的项目成为可能。


这是一厢情愿。这不是真的


  

我是不是正确使用这个容器?


我们不能告诉,因为你不告诉你是如何使用它。 (只有片段)


  

时的这个问题应该发生在纯矢量状容器?是员工很少使用,所以它是越野车?


再一厢情愿


  

当我使用它与向量我可能会面临哪些注意事项?


同样作为与其它类型的:你有没有违反线程不变?你从1单一线程读取,并从一张单人床其他线程写?

更新

感谢您将SSCCE。

这的确是在使用一个微妙的bug(我错过了前面)。你传递的指针和队列元素类型的不可以转换到这一点。

这意味着,单值超载不适用。

超载是采取的是单参数超载服用的 输出迭代


 模板< typename的输出迭代>
  提高:: disable_if<类型名is_convertible< T,输出迭代> ::类型,SIZE_TYPE> ::类型
  POP(输出迭代它);


  
  

中弹出对象的输出迭代器它


  
  

注意


  
  

      
  • 线程安全的,无等待

  •   

  
  

要求:


  
  

      
  • 只有一个线程被允许数据弹出到spsc_queue

  •   

  
  

返回:


  
  

杀出项目数


哎呀。你调用UB存在,因为它会分配给&安培;价值+ 1 等,这确实引起的std ::矢量::分配大声地哭了起来。

I've tried to use boost::lockfree::spsc_queue. Occasinally found segfault, when used it with std::vector<uint16_t>. The whole type is:

spsc_queue<vector<uint16_t>, boost::lockfree::capacity<1000>> queue;

Debugging showed, that segfault happend on pop, which was organized this way:

vector<uint16_t> vec;
while(!done)
  while(quuee.pop(&vec)){ /* do staff */}

Documentation on spsc_queue says:

bool pop(T & ret);

Pops one object from ringbuffer.

But, when I use

vector<uint16_t> vec;
while(!done)
  while(quuee.pop(&vec,1)){
    /* do staff */
  }

Segfault magically disappears. This makes me thinking, that bool pop(T&) somehow tries to restore as many items as possible.

Am I correctly use this container? Is this issue should happen to vector-like containers only? Is that staff rarely used, so it is buggy? What caveats I could face when I'm using it with vectors?

解决方案

Segfault magically disappears. This makes me thinking, that bool pop(T&) somehow tries to restore as many items as possible.

That's magical thinking. It's not true

Am I correctly use this container?

We can't tell because you don't show how you're using it. (Only a fragment)

Is this issue should happen to vector-like containers only? Is that staff rarely used, so it is buggy?

Magical thinking again

What caveats I could face when I'm using it with vectors?

The same as with other types: Did you violate the threading invariants? Do you read from 1 single thread, and write from 1 single other thread?

UPDATE

Thanks for adding a SSCCE.

It was indeed a subtle bug in usage (I missed it earlier). You're passing a pointer to value and the queue element type is not convertible to that.

That means that the single-value overload is not applicable.

The overload that is taken is the the single-argument overload taking an OutputIterator.

template<typename OutputIterator> 
  boost::disable_if< typename is_convertible< T, OutputIterator >::type, size_type >::type 
  pop(OutputIterator it);

Pops objects to the output iterator it

Note

  • Thread-safe and wait-free

Requires:

  • only one thread is allowed to pop data to the spsc_queue

Returns:

number of popped items

Oops. You're invoking UB there as it will assign to &value+1 etc. This was indeed causing std::vector::assign to cry loudly.

这篇关于升压SPSC队列段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆