旋转循环与缓存小姐:哪个更昂贵?
对于在大小大于指针大小的整数上的原子操作,使用旋转循环实现它是否更好,或者最好动态分配对象并使用CAS更新它?
我知道动态分配对象会增加缓存失误的机会,但是使用自旋环也会降低并发。请哪一个更昂贵,还是取决于对象的大小?我认为当对象大于指针的大小不超过3倍时,旋转环应该很好。我可以假设这个吗?
*****************************************
我的意思是在这里是一个相互排斥的自旋锁。在第一种情况下,我们使用一种悲观的控制机制,在该机制中,我们获得了以读取,设置或更新对象的锁定。
在第二种情况下,我们动态分配对象并制作其指针原子。读取仅是指针的原子负载,然后将其删除。商店将动态分配新值,然后执行指针的原子商店。对于更新操作,我们使用一种乐观的方法来首次阅读指针,计算结果并动态分配结果,然后尝试使用CAS更新原子指针。
使用相互排除的自旋锁会减少并发性,但是如果我动态分配对象,我担心缓存失踪的成本。在什么情况下,我们可以选择每种方法?
For atomic operations on a integers whose size is bigger larger than the size of a pointer, is it better to use a spin loop to implement it or is it better to dynamically allocate the object, and use CAS to update it?
I know that dynamically allocating the object increases the chance for a cache miss, but then using a spin-loop also reduces concurrency. Please which one is costlier, or does it depend on the size of the object? I'm thinking that a spin-loop should be fine for when the object is not more than 3X bigger than the size of a pointer. Am I right to assume this?
*************** edit ****************
What I mean by spin-loop here is a mutual exclusion spin-lock on the object. In the first scenario we use a pessimistic control mechanism where we obtain the lock to read, set or update the object.
In the second scenario, we dynamically allocate the object and make its pointer atomic. A read would just be an atomic load of the pointer and then dereferencing it. A store would be to dynamically allocate the new value, and then perform an atomic store of its pointer. For update operations, we use an optimistic approach of first reading the pointer, calculate the result and dynamically allocate it and then try to update the atomic pointer using CAS.
Using a mutual exclusion spin-lock would reduce concurrency but I'm worried about the cost of a cache-miss if I dynamically allocate the object. In what situations might we choose each approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论