什么是“锁是昂贵的操作"的原因?经常被说出来吗? [英] What is the reason for "locks are an expensive operation" to be uttered so often?

查看:95
本文介绍了什么是“锁是昂贵的操作"的原因?经常被说出来吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关线程的资料,以及涉及的所有同步机制.我也了解未正确执行操作的危险.

I have read a lot of material on threading, and all the synchronization mechanisms involved. I also understand the dangers of not doing it properly.

我刚刚观看了 PDC 2009视频关于并行性和并发性,但这还没有再次提到锁是昂贵的操作".现在,我在各种教科书中都遇到过这样的短语,而且我听说该领域的专家也这么说过.

I just watched this PDC 2009 video about Parallelism and Concurrency, and here is yet again this mention that "locks are an expensive operation". I've now come across a phrase like this in various texts, books, and I've heard experts in the field say it too.

我想知道,获取锁(互斥锁或信号灯)到底有什么昂贵?它是否导致在汇编程序级别发出LOCK#指令的事实?

I was wondering, what exactly is so expensive about obtaining a lock (mutex or semaphore)? Is it the fact that it causes a LOCK# instruction to be emitted at Assembler level?

获取锁是否需要对操作系统进行内核调用?

Is it the fact that obtaining a lock requires a kernel call into the OS?

为什么锁被认为是昂贵的操作? 昂贵"是一个相对相对的术语,因此,如果与创建一个新线程(需要建立线程堆栈等)进行比较,那么获得锁的成本到底有多高?

Why are locks considered to be an expensive operation? "Expensive" is a fairly relative term, so if we compared to creating a new thread (which requires setting up a thread stack, etc), how expensive is obtaining a lock really?

被子下面发生了什么?

What goes on underneath the covers?

我的猜测是,它不可能那么昂贵,因为我确定要运行Windows(例如),必须一直使用数百种锁/同步机制.

My guess is, that it cannot possibly that expensive, because I'm sure for Windows (for example) to run, hundreds of locks/synch mechanisms have to be used all the time.

有人可以详细说明吗?

Can anyone elaborate?

注意:我只是很好奇,我知道线程是如何工作的,我也不想做一些愚蠢的优化.

NOTE: I'm merely curious, I know how threading works, and I'm not looking to do some silly optimization either.

推荐答案

它是否导致在汇编程序级别发出LOCK#指令的事实?

Is it the fact that it causes a LOCK# instruction to be emitted at Assembler level?

不,因为它并不总是这样做.

No, since it doesn't always do that.

获取锁是否需要对操作系统进行内核调用?

Is it the fact that obtaining a lock requires a kernel call into the OS?

不,因为它通常不这样做.

No, since it typically doesn't do that.

实际上,锁非常非常便宜.这是争用,这很昂贵.如果必须在锁定和争用之间进行选择,那么大多数情况下,锁定是一个更好的选择.

In fact, locks are very, very inexpensive. It's contention that's expensive. If you have to choose between a lock and contention, most of the time the lock is a better option.

锁,如果使用得当,是一种争用 avoidance 机制.它们会自动找到争用线程并对其进行调度,从而使线程主要结束于不争用并发运行的线程.

Locks, when used properly, are a contention avoidance mechanism. They automatically find threads that contend and de-schedule them such that one winds up primarily with threads that do not contend running concurrently.

例如:假设您有四个可以运行的线程,分别是 A B C D .说 A B 相互竞争(例如,他们操纵相同的集合).并且说 C D 相互竞争,但是 A 不与 C 竞争.如果 A B 同时运行(竞争),则锁将导致其中之一无法运行,调度程序将调度 C (或 D ),这两个线程将在没有进一步争用的情况下运行. (至少直到下一个上下文切换为止.)

For example: Say you have four threads that are ready to run, A, B, C, and D. Say A and B contend with each other (say they manipulate the same collection). And say C and D contend with each other, but A doesn't contend with C. If A and B are running at the same time (contending), the locks will cause one of them to not be ready to run, the scheduler will then schedule C (or D), and the two threads will run without further contention. (At least until the next context switch.)

通常,当人们说锁很贵"时,它们意味着争执很昂贵.不幸的是,通过用自己的方式表述它,他们经常鼓励人们最小化锁,但增加了过程中的争用.在绝大多数情况下,这是一个失败的主张. (有一些例外.)

Usually, when people say "locks are expensive", they mean that contention is expensive. Unfortunately, by phrasing it the way they do, they often encourage people to minimize locks but increase contention in the process. That is a losing proposition in the vast majority of cases. (There are a few exceptions.)

这篇关于什么是“锁是昂贵的操作"的原因?经常被说出来吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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