提高:: lockfree :: spsc_queue忙等待的策略。有没有阻止弹出? [英] boost::lockfree::spsc_queue busy wait strategy. Is there a blocking pop?

查看:1311
本文介绍了提高:: lockfree :: spsc_queue忙等待的策略。有没有阻止弹出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用一个的boost :: lockfree :: spec_queue 通过两个boost_threads运行两个对象的函子在我的应用程序进行通信。

So i'm using a boost::lockfree::spec_queue to communicate via two boost_threads running functors of two objects in my application.

一切都只是一个事实,即 spec_queue :: POP()方法是非阻塞的罚款。它返回真或假,即使有什么在队列中。但是我的队列似乎总是返回True(#1问题)。我想这是因为我preallocate队列中。

All is fine except for the fact that the spec_queue::pop() method is non blocking. It returns True or False even if there is nothing in the queue. However my queue always seems to return True (problem #1). I think this is because i preallocate the queue.

typedef boost::lockfree::spsc_queue<q_pl, boost::lockfree::capacity<100000> > spsc_queue;

这意味着,使用队列有效我要忙等待弹出不断使用100%的CPU队列中。宁愿不睡觉的时间任意金额。我用在Java其他队列阻断,直到一个对象变为可用。可以这样性病完成::或升压::数据结构?

This means that to use the queue efficiently i have to busy wait constantly popping the queue using 100% cpu. Id rather not sleep for arbitrary amounts of time. I've used other queues in java which block until an object is made available. Can this be done with std:: or boost:: data structures?

推荐答案

一个无锁队列,顾名思义,没有阻塞操作。

A lock free queue, by definition, does not have blocking operations.

您如何对数据结构的同步?没有内部锁,原因很明显,因为这将意味着所有的客户需要在其上同步,使你的祖父锁定并发队列。

How would you synchronize on the datastructure? There is no internal lock, for obvious reasons, because that would mean all clients need to synchronize on it, making it your grandfathers locking concurrent queue.

确实如此,你必须给自己制定一个等待功能。你是怎么做到这取决于你的使用情况,这可能是为什么图书馆不提供一(免责声明:我没有检查,我不声称知道完整的文档)。

So indeed, you will have to devise a waiting function yourself. How you do this depends on your use case, which is probably why the library doesn't supply one (disclaimer: I haven't checked and I don't claim to know the full documentation).

所以,你能做些什么:


  • 正如你已经描述的,你可以在一个紧密的循环旋转。显然,如果你知道你的等待状态(队列非空)总是要满足非常快,你会做到这一点。

  • As you already described, you can spin in a tight loop. Obviously, you'll do this if you know that your wait condition (queue non-empty) is always going to be satisfied very quickly.

替代地,轮询队列在一定的频率(在平均时间做微休眠)。安排一个好良好的频率是一门艺术:对于某些应用100ms的是最优的,为他人,一个潜在的100ms的等待会破坏吞吐量。因此,各不相同的和衡量性能指标(不要忘了功耗,如果你的应用程序将是对多核心的数据中心部署的:)。)

Alternatively, poll the queue at a certain frequency (doing micro-sleeps in the mean time). Scheduling a good good frequency is an art: for some applications 100ms is optimal, for others, a potential 100ms wait would destroy throughput. So, vary and measure your performance indicators (don't forget about power consumption if your application is going to be deployed on many cores in a datacenter :)).

最后,可以得出一个混合溶液,纺丝对于固定数量的迭代,以及诉诸(增加)间隔轮询,如果没有到达。其中,高负荷发生在阵阵这将很好地支持服务器的应用程序。

Lastly, you could arrive at a hybrid solution, spinning for a fixed number of iterations, and resorting to (increasing) interval polling if nothing arrives. This would nicely support servers applications where high loads occur in bursts.

这篇关于提高:: lockfree :: spsc_queue忙等待的策略。有没有阻止弹出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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