Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In the general case, a single global lock is less efficient (more contention) but safer (no risk of deadlock) as long as it's a <code>RLock</code> (reentrant) rather than a plain <code>Lock</code>.</p> <p>The potential problems come when a thread that's executing while holding a lock tries to acquire another (or the same) lock, for example by calling another method that contains the <code>acquire</code> call. If a thread that's already holding a lock tries to acquire it again, it will block forever if the lock's a plain <code>Lock</code>, but proceed smoothly if it's a slightly more complex <code>RLock</code> -- that's why the latter is called <em>reentrant</em>, because the thread holding it can "enter" (acquire the lock) again. Essentially, a RLock keeps track of <em>which</em> thread holds it, and how many time the thread has acquired the lock, while the simpler Lock does not keep such information around.</p> <p>With multiple locks, the deadlock problem comes when one thread tries to acquire lock A then lock B, while another tries to acquire first lock B, then lock A. If that occurs, then sooner or later you'll be in a situation where the first lock holds A, the second one holds B, and each tries to acquire the lock that the other one is holding -- so both block forever.</p> <p>One way to prevent multiple-lock deadlocks is to make sure that locks are always acquired in the same order, whatever thread is doing the acquiring. However, when each instance has its own lock, that's exceedingly difficult to organize with any clarity and simplicity.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload