stm实现问题
嘿伙计们,
在某些时候,我认为这些 stm 实现(我使用过一点点的多元宇宙......)被过度炒作了。因为在某些时候他们使用 CAS,这为他们提供了操作的原子性。如果我直接使用 CAS 而不是使用这些实现会怎样?虽然我同意这些实现也可能提供其他功能,但是如果我可以获得相同的性能并且没有很多功能可以使用,那么我应该直接使用 CAS 而不是使用 multi-verse 或 scala 或其他实现吗?< br> 嘿,伙计们,当您使用这些 stm 实现而不是 CAS 时,您是否注意到任何性能提升?因为当我运行(在multiverse doc和atomicInteger JAVA中给出)atomicCounter时,我在atomicInteger中获得了比在multiverse中更好的性能。那么,stm 的基础是 CAS 吗? _
Hey guys,
At some point i think that these stm implementation ( multiverse which i have used a little bit... ), are over-hyped. Because at some point they uses CAS which is providing them atomicity of operations. What if i use CAS directly instead of using these implementation ? Though i agree that these implementation might be providing others features too, but If i can gain same performance and don't have a lot of features to use then should i use CAS directly instead of using multi-verse or scala or other implementations ?
Hey guys have you noticed any performance gain when you use those stm implementation than CAS ? since when i run ( given in multiverse doc and in atomicInteger JAVA) atomicCounter i gain better performance in atomicInteger than in multiverse. So is it like _the base of stm is CAS ? _
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
STM 可以构建在许多不同的同步原语之上,但经常使用 CAS,因为它是最简单、最轻量级的选项,不会施加太多不必要的语义约束。
但是,是的,仅仅使用 CAS 操作会比使用除其他之外执行 CAS 操作的东西更快。
但它们有不同的目的。 CAS 允许您自动更新一些选择数据类型,STM 通常可用于任意类型。 STM 为您提供了更大事务范围的原子性(如果您的事务修改了 4 个不同的变量,则所有 4 个变量都会作为同一个原子操作提交。单个 CAS 只会原子地更新一个对象),并且它给出CAS 不存在隔离性和一致性保证。
最终,你无法将两者进行比较。这就像将车轮与汽车进行比较一样。是的,车轮更小、更轻,但那是因为它不提供与汽车相同的功能。
STM can be built on top of many different synchronization primitives, but CAS often used, because it's the simplest, most lightweight option which doesn't impose too many unnecessary semantic constraints.
But yes, just using a CAS operation is going to be faster than using something which, among other things performs a CAS operation.
But they serve different purposes. CAS allows you to atomically update a few select datatypes, STM can typically be used on arbitrary types. STM gives you atomicity on the much larger transaction scope (if your transaction modifies 4 different variables, all 4 are committed as the same atomic operation. A single CAS will only atomically update one object), and it gives you isolation and consistency guarantees that don't exist with CAS.
Ultimately, you can't compare the two. It's like comparing a wheel to a car. Yes, a wheel is smaller and more lightweight, but that's because it doesn't offer the same functionality as the car does.
您可以这样做,但对于大多数问题来说这都太低级了。转向 CAS 就像用汇编语言编写所有内容 — 当然,您可以做到,但这并不能很好地利用您的时间。
挑战在于找到更准确地匹配问题抽象级别的东西,而不是计算机的抽象级别。
显然,在底层,任何软件TM都必须根据硬件TM或原子锁定操作来实现。回滚和避免冲突的附加语义可以单独完成。如果您只需要原子比较和交换,那么 STM 就足够了。下拉并使用互斥锁或信号量,或者更接近 CAS 指令的其他共享内存抽象。
You could do that, but it is too low-level for most problems. Dropping down to CAS is like writing everything in assembly -- sure, you can do it, but it isn't a good use of your time.
The challenge is to find something that more accurately matches the level of abstraction of the problem, not of the computer.
Obviously, at the bottom any software TM must be implemented in terms of either hardware TM or an atomic locking operation. The additional semantics of rollback and collision avoidance can be done separately. If you only need atomic compare-and-swap, then STM is more than you need. Drop down and use a mutex or sempaphore, or some other shared memory abstraction closer to the CAS instruction.