开发者

Mutex Unlock using add

开发者 https://www.devze.com 2023-01-26 17:32 出处:网络
While checking Mutex unlock codes around, I found some that add 1 into the mutex variable instead of setting it to \"1\" directly. Is there any pros/con开发者_Python百科s of this?

While checking Mutex unlock codes around, I found some that add 1 into the mutex variable instead of setting it to "1" directly. Is there any pros/con开发者_Python百科s of this?

Thanks


I wonder if it is possible that you are refereeing to the difference between mutex and semaphore resource access control.


Edit

That's all about CPU cycles needed for those two OPs. To my knowledge add uses less CPU cycles than mov. But then again it is very arch-dependent and questionable. Also, bear in mind that assemblers choice of how to encode a higher-level language instruction is very dependent on the surrounding instructions


It's just important that whatever operation is used, that it is atomic. It makes most sense to me to doing a set, rather than an add, particularly if there's a test-and-set instruction or implementation.

I found this implementation of a TestAndSet function for the x86 architecture. Here it uses a set (mov) instruction but it could have also used add or inc to do this but it would require eax to be 0 with the xchg instruction being used for atomicity. I suppose that requiring eax to be zero could be a con.

0

精彩评论

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