排他锁和共享锁有什么区别? [英] What's the difference between an exclusive lock and a shared lock?
问题描述
根据Wikipedia
共享锁有时称为读锁",排他锁有时称为写锁".
您能解释一下共享"和专有"术语背后的原因吗?
我把这个答案写下来是因为我认为这会很有趣(而且很合适):</p>
在一个教室里,把一个可锁定的对象想成一个 blackboard (可锁定),其中包含一个 teacher (作家)和许多 students (读者) ).
当老师在黑板上写东西(排他锁)时:
-
没有人可以读取它,因为它仍在被写入,并且她阻止了您的视图=> 如果对象被排他地锁定,则无法获得共享锁.
> li> -
其他老师也不会上课,也不会开始写作,否则黑板会变得不可读,并使学生感到困惑=> 如果某个对象被排他锁,则无法获得其他排他锁..
当学生正在阅读(共享锁)板上的东西时:
-
他们所有人都可以读取其中的内容,一起=> 多个共享锁可以共存.
-
在老师让他们完成阅读之前,她要清空白板以写更多=> 如果已经存在一个或多个共享锁,则将无法获得排他锁.
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:
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.
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:
They all can read what is on it, together => Multiple shared locks can co-exist.
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屋!