原子递减是否比递增更昂贵? [英] Is atomic decrementing more expensive than incrementing?

查看:94
本文介绍了原子递减是否比递增更昂贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在他的博客Herb Sutter 中写道

[...],因为增加了智能指针引用计数 通常可以优化为与普通增量相同 在优化的shared_ptr实现中-只是一条普通的增量指令, 并在生成的代码中没有栅栏.

[...] because incrementing the smart pointer reference count can usually be optimized to be the same as an ordinary increment in an optimized shared_ptr implementation — just an ordinary increment instruction, and no fences, in the generated code.

但是,减量必须是原子减量或同等的量, 这会生成特殊的处理器内存指令,这些指令在 自己,最重要的是会引起记忆 限制优化周围代码的限制.

However, the decrement must be an atomic decrement or equivalent, which generates special processor memory instructions that are more expensive in themselves, and that on top of that induce memory fence restrictions on optimizing the surrounding code.

文本是关于shared_ptr的实现的,我不确定他的言论是仅适用于此情况还是一般情况.从他的表述中可以得出,通常是 .

The text is about the implementation of shared_ptr and I am not sure if his remark applies only on this or is generally the case. From his formulation I gather it is generally.

但是考虑到这一点,我只能想到当if(counter==0)紧随其后的情况下更昂贵的减价",而shared_ptr可能就是这种情况.

But when I think about it I can only think of "more expensive decrement" when a if(counter==0) immediately follows -- which probably is the case with shared_ptr.

因此,我想知道 atomic 操作++counter是否(通常)比--counter总是总是快 ,或者仅仅是因为将if(--counter==0)...shared_ptr一起使用?

Therefore I wonder if the atomic operation ++counter is (usually) always faster than --counter, or just because it is used if(--counter==0)... with shared_ptr?

推荐答案

我认为这是指可以隐藏"增量的事实,其中减量和校验"必须作为一项操作来完成.

I believe it's referring to the fact that the increment can be "hidden", where the "decrement and check" has to be done as one operation.

我不知道--counter(或counter--,假设我们正在谈论的是简单数据类型,例如int,char等)比++countercounter++慢的任何体系结构.

I'm not aware of any architecture where --counter (or counter--, asssuming we're talking about simple datatypes like int, char, etc) is slower than ++counter or counter++.

这篇关于原子递减是否比递增更昂贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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