开发者

try-lock mutex call vs CAS calls

开发者 https://www.devze.com 2023-04-03 08:23 出处:网络
As the mutex in most of the systems are implemented using CAS ops, I was wondering about performance comparison of these two constructs.

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开发者_JAVA百科 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.


It is not fair to say anything about the relative performance of CAS vs a lock. Different OS implement locks in very different ways. Win32 needs to context switch into the kernel which is slow, Linux has user space mutexes. The performance will also vary greatly depending on how contended the critical section is. You also need to consider what you're doing in the critical section, are you incrementing one integer or doing a complex operation? So there are many variables that go into the relative performance and you can't make broad statements about it.

I would recommend using your language/platforms easiest to use locking abstraction. Measure the performance and see if it is acceptable to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号