排他锁和共享锁有什么区别? [英] What's the difference between an exclusive lock and a shared lock?

查看:1031
本文介绍了排他锁和共享锁有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Wikipedia

共享锁有时称为读锁",排他锁有时称为写锁".

您能解释一下共享"和专有"术语背后的原因吗?

解决方案

我把这个答案写下来是因为我认为这会很有趣(而且很合适):<​​/p>

在一个教室里,把一个可锁定的对象想成一个 blackboard (可锁定),其中包含一个 teacher (作家)和许多 students (读者) ).

当老师在黑板上写东西(排他锁)时:

  1. 没有人可以读取它,因为它仍在被写入,并且她阻止了您的视图=> 如果对象被排他地锁定,则无法获得共享锁.

    li>
  2. 其他老师也不会上课,也不会开始写作,否则黑板会变得不可读,并使学生感到困惑=> 如果某个对象被排他锁,则无法获得其他排他锁..

当学生正在阅读(共享锁)板上的东西时:

  1. 他们所有人都可以读取其中的内容,一起=> 多个共享锁可以共存.

  2. 在老师让他们完成阅读之前,她要清空白板以写更多=> 如果已经存在一个或多个共享锁,则将无法获得排他锁.

According to wikipedia,

Shared locks are sometimes called "read locks" and exclusive locks are sometimes called "write locks".

Can you explain the reasoning behind the terms "shared" and "exclusive"?

解决方案

I wrote this answer down because I thought this would be a fun (and fitting) analogy:

Think of a lockable object as a blackboard (lockable) in a class room containing a teacher (writer) and many students (readers).

While a teacher is writing something (exclusive lock) on the board:

  1. Nobody can read it, because it's still being written, and she's blocking your view => If an object is exclusively locked, shared locks cannot be obtained.

  2. Other teachers won't come up and start writing either, or the board becomes unreadable, and confuses students => If an object is exclusively locked, other exclusive locks cannot be obtained.

When the students are reading (shared locks) what is on the board:

  1. They all can read what is on it, together => Multiple shared locks can co-exist.

  2. The teacher waits for them to finish reading before she clears the board to write more => If one or more shared locks already exist, exclusive locks cannot be obtained.

这篇关于排他锁和共享锁有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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