升级readlock的优势在哪里? [英] Advantage of upgradable readlock?

查看:86
本文介绍了升级readlock的优势在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是什么使用可升级读锁,而不是执行这些步骤的优点:





  1. 取读锁

  2. 检查状态,看看我们是否需要采取写锁定

  3. 释放读锁定

  4. 以写锁定

  5. 执行更新

  6. 释放写锁定




的执行上述步骤的一个明显的缺点,而不是采取可升级读锁定是,存在的时间的步骤3和4,其中另一个线程可以占用之间的窗口写锁。



除了这个优势,你发现什么其他的优势,采取升级读锁在我上面提到的步骤?


解决方案

让我们考虑一个可以使用没有一个单独的升级读者一个读写锁的不同方法。



通过你的模式,有3及4正如你指出,在另一个线程可以把作家锁之间的竞争。更重要的是,有3和4之间的一个步骤,其中一个线程可以把作家锁并改变我们在第2步



看到的状态

因此,我们已经有了这取决于如何LIKEY这四个选择是发生:




  1. 我们留在你的方法因为这实际上是不可能的(例如,一个给定的状态转变是在我们的应用单向的,所以一旦观察到它是永久的)。在这种情况下,尽管我们可能很可能已经改造,以便不需要锁定在所有。 (单向转换借给自己无锁技术)。


  2. 我们只取摆在首位作家锁,因为国家,我们在步骤观察2很可能会改变,这是浪费时间与读者锁检查它


  3. 我们改变你的步骤:




    1. 取读锁

    2. 检查状态,看看我们是否需要采取写锁定

    3. 发行读锁定

    4. 以写锁定

    5. 重新检查条件的情况下,它改变了。

    6. 执行更新

    7. 释放写锁定


  4. 我们更改为:




    1. 采取一个递归支撑锁的读锁。

    2. 检查,看看是否我们需要写锁

    3. 以写锁定(没有释放读)。

    4. 执行更新。

    5. 释放写锁

    6. 释放读锁。




不难看出为什么4更诱人一些,虽然它只是稍微很难看到它如何使死锁容易创建。可悲的是,稍硬仅仅是足以让很多人看到了优势,没有看到缺点。



有关谁不发现它,如果两个线程都读锁,其中一个升级为写锁定它必须等待另一个以释放读锁定。然而,如果说第二个线程升级到写锁不释放读锁,然后它会永远等待第一个线程,这将永远等待它。






正如上面所说,只是哪种方法最好取决于它有多大的可能性为国家在此期间(或如何及时,我们要对其做出反应,我想)改变。即使在非脱模升级的最后的方法可以有它在可行的码的地方,只要有永远只能是有史以来试图不释放升级其锁定一个线程。



除了其中最后选项的工作,其他的选项之间的差异都是关于性能,并且这是最高效的主要取决于重成本的特例检查状态和写的可能性被中止,由于在此期间发生了变化。



不过,请注意,它们都涉及结束一个作家锁,所以所有的他们拥有阻止所有读取线程,即使写确实中止的效果。



可升级读锁给我们一个中间路线,因为虽然他们阻止写锁和其他升级读锁,它们不会阻塞读锁。他们也许是更好的,虽然可以作为那些尚未承诺写一写锁升级还不如读锁。*当它决定不升级,在读线程效果的情况下是零。



这意味着如果该线程将决定不更改该州的甚至略有可能的话,读线程不会受到影响,而且性能改善肯定可以证明它的使用。



*对于这个问题,读写器是一个有点用词不当,我们可以如保护整数或物体与 ReaderWriterLockSlim ,使用读锁读取和原子写入单个项目,并使用写锁操作的数组需要读取整个数组不改变,因为它读取它的一部分。在这样的情况下,它是一个读出操作比所需要的排他锁,而写操作是精细的共享锁。


I was wondering what are the advantages of using a upgradable read lock as opposed performing these steps:

  1. Take read lock
  2. Check condition to see if we need to take write lock
  3. Release Read Lock
  4. Take Write Lock
  5. Perform update
  6. Release Write Lock

One apparent disadvantage of the performing the above steps as opposed taking an upgradable read lock is, that there is an window of time between the steps 3 and 4, where another thread can take up a write lock.

Apart from this advantage what other advantages do you find for taking upgradable read lock over the steps I have mentioned above?

解决方案

Let's consider the different ways in which one can use a reader-writer lock that doesn't have a separate "upgradable reader".

With your pattern, there is a race between step 3 and 4 as you point out, where another thread can take the writer lock. More to the point, there is a step between 3 and 4 where a thread can take the writer lock and change the state we observed in step 2.

Therefore, we've got four choices depending on how likey this is to happen:

  1. We stay with your approach because this is actually impossible (e.g. a given state transition is one-way in our application, so once observed it is permanent). In this case though we could quite possibly have remodelled so as to not need a lock at all. (One-way transitions lend themselves to lock-free techniques).

  2. We just take the writer lock in the first place, because the state we observe in step 2 is very likely to change and it's a waste of time checking it with a reader lock.

  3. We change your steps to:

    1. Take read lock
    2. Check condition to see if we need to take write lock
    3. Release Read Lock
    4. Take Write Lock
    5. Re-check the condition in case it changed.
    6. Perform update
    7. Release Write Lock

  4. We change to:

    1. Take a read lock on a recursion-supporting lock.
    2. Check to see if we need to take write lock.
    3. Take write lock (no release of read).
    4. Perform update.
    5. Release write lock.
    6. Release read lock.

It's not hard to see why 4 was more tempting to some, though it is only slightly harder to see how it makes deadlocks easy to create. Sadly, that slightly harder is just enough for a lot of people to see the advantages without seeing the disadvantages.

For anyone who doesn't spot it, if two threads have a read lock and one of them upgrades to a write lock it must wait for the other to release the read-lock. However if that second thread upgrades to a write lock without releasing the read-lock then it will wait forever on the first thread, which will wait forever on it.


As said above, just which approach is best depends on how likely it is for the state to change in the meantime (or how promptly we want to react to it, I suppose). Even the last approach with the non-releasing upgrade can have its place in viable code, as long as there can only ever be one thread that ever tries to upgrade its lock without releasing.

Aside from the special case where the last option works, the difference between the other options are all about performance, and which is most performant mostly depends on the cost of re-checking the state and the likelihood of the write being aborted due to a change in the meantime.

However, note that all of them involve taking a writer lock, so all of them have the effect of blocking all reading threads, even when the write is indeed aborted.

Upgradable read-locks give us a middle-ground because while they block write-locks and other upgradable read-locks, they don't block read locks. They are perhaps better though of not as read-locks that can upgrade as a write-locks that have yet to commit to writing.* In the cases where it was decided not to upgrade, the effect on the reading threads is nil.

This means that if it's even slightly possible that the thread will decide not to change the state, the reading threads are not affected, and the performance improvement can certainly justify it's use.

*For that matter, "reader-writer" is a bit of a misnomer, we could e.g. protect an array of ints or objects with a ReaderWriterLockSlim, use the read-locks for both reading and writing individual items atomically and use the write-locks for operations that need to read the entire array without parts of it changing as it reads. In such a case it's a reading operation than needs the exclusive lock, while writing operations are fine with the shared lock.

这篇关于升级readlock的优势在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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