try_lock在互斥锁上的效率如何? [英] How efficient is a try_lock on a mutex?

查看:144
本文介绍了try_lock在互斥锁上的效率如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try_lock在互斥锁上的效率如何? IE.在两种可能的情况下(例如,互斥锁之前已经被锁定,或者它是空闲的并且可以被锁定),它们可能有多少个汇编程序指令以及它们消耗多少时间.

How efficient is a try_lock on a mutex? I.e. how much assembler instructions are there likely and how much time are they consuming in both possible cases (i.e. the mutex was already locked before or it was free and could be locked).

如果您在回答问题时遇到问题,请执行以下操作(如果确实不清楚):

In case you have problems to answer the question, here is a how to (in case that is really unclear):

如果该答案在很大程度上取决于操作系统的实现和硬件:请针对常见的OS(例如Linux,Windows,MacOSX),它们的最新版本(以防它们与早期版本有很大差异)进行回答.硬件(x86,amd64,ppc,arm).

If that answer depends a lot on the OS implementation and hardware: Please answer it for common OS`s (e.g. Linux, Windows, MacOSX), recent versions of them (in case they differ a lot from earlier versions) and common hardware (x86, amd64, ppc, arm).

如果这还取决于库,请以pthread为例.

If that also depends on the library: Take pthread as an example.

如果它们确实完全不同,也请回答.如果它们不同,请说明不同之处. IE.他们有什么不同之处?周围有哪些通用算法?周围是否有不同的算法,或者所有常见的系统(如果不清楚,在上面的列表中是常见的)是否都以相同的方式实现了互斥锁?

Please also answer if they really differ at all. And if they differ, please state the differences. I.e. what do they do differently? What common algorithms are there around? Are there different algorithms around or do all common systems (common by the above list if that is unclear) have implemented mutexes just in the same way?

截至此Meta讨论,真的应该是一个单独的问题.

As of this Meta discussion, this really should be a separate question.

此外,我作为

Also, I have asked this as a separate question from the performance of a lock because I am not sure if try_lock may behave different. Maybe also depending on the implementation. Then again, please answer it for common implementations. And this very similar/related question obviously shows that this is an interesting question which can be answered.

推荐答案

互斥体是独立于任何实现的逻辑构造.因此,对互斥锁的操作既没有效率也没有效率,它们的定义很简单.

A mutex is a logical construction that is independent of any implementation. Operations on mutexes therefore are neither efficient nor inefficient - they are simply defined.

因此,您的问题类似于询问汽车的效率如何?",而没有提及您可能在谈论哪种汽车.

Your question is therefore akin to asking "How efficient is a car?", without reference to what kind of car you might be talking about.

我可以在现实世界中使用烟雾信号,信鸽或铅笔和纸来实现互斥.我也可以在计算机上实现它们.我可以在Cray 1,Intel Core 2 Duo或地下室486上通过某些操作来实现互斥锁.我可以在硬件中实现它们.我可以在操作系统内核或用户空间中的软件中实现它们,也可以使用两者的某种组合来实现它们.我可能会使用保证在关键部分无冲突的无锁算法来模拟互斥锁(但不实现它们).

I could implement mutexes in the real world with smoke signals, carrier pigeons or a pencil and paper. I could also implement them on a computer. I could implement a mutex with certain operations on a Cray 1, on an Intel Core 2 Duo, or on the 486 in my basement. I could implement them in hardware. I could implement them in software in the operating system kernel, or in userspace, or using some combination of the two. I might simulate mutexes (but not implement them) using lock-free algorithms that are guaranteed conflict-free within a critical section.

您的后续编辑对情况无济于事. 使用低级语言(例如C或其他语言)"几乎是无关紧要的,因为那时我们正要评估语言的实现性能,这充其量只是一个滑坡. "[p] rom pthread或本机系统库提供的任何内容"同样无济于事,因为正如我说的那样,可以通过多种方法在不同环境中实现互斥锁,甚至无法做一个有用的比较.

Your subsequent edits don't help the situation. "In a low level language (like C or whatever)" is mostly irrelevant, because then we're into measuring language implementation performance, and that's a slippery slope at best. "[F]rom pthread or whatever the native system library provides" is similarly unhelpful, because as I said, there are so many ways that one could implement mutexes in different environments that it's not even a useful comparison to make.

这就是为什么您的问题无法回答的原因.

This is why your question is unanswerable.

这篇关于try_lock在互斥锁上的效率如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆