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 ? _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.
What if i use CAS directly instead of using these implementation?
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.
精彩评论