As the mutex in most of the systems are implemented using CAS ops, I was wondering about performance comparison of these two constructs.
Is it fair to say that if a mutex is implemented using CAS, then the try-lock call on that mutex will be same/similar performance compare to CAS operations ?
CAS, being highly system dependent, I was thinking if it could be summarily replaced with the more well-known/standardized derivation of it, mutex try-lock.
Your reasoning is sound; on any sane implementation, the cost of a “trylock” operation will be roughly the same as a CAS. However, CAS in general cannot be replaced by trylock; trylock is a weaker primitive that can’t manipulate arbitrary data.