@@ -168,34 +168,37 @@ object) and ``isExpired()`` (which returns a boolean).
168
168
Shared Locks
169
169
------------
170
170
171
- Sometimes, a data structure cannot be updated atomically and is invalid during
172
- the time of the update. In this situation, other process should not read or
173
- write the data until the update is complete. But once updated, multiple process
174
- can read the data in parallel .
171
+ .. versionadded :: 5.2
172
+
173
+ Shared locks (and the associated `` acquireRead() `` method and
174
+ `` SharedLockStoreInterface ``) were introduced in Symfony 5.2 .
175
175
176
- In this situation, a common solution is to use shared lock which allows
177
- concurent access for read-only operations, while write operations require
178
- exclusive access.
176
+ A shared or `readers–writer lock `_ is a synchronization primitive that allows
177
+ concurrent access for read-only operations, while write operations require
178
+ exclusive access. This means that multiple threads can read the data in parallel
179
+ but an exclusive lock is needed for writing or modifying data. They are used for
180
+ example for data structures that cannot be updated atomically and are invalid
181
+ until the update is complete.
179
182
180
183
Use the :method: `Symfony\\ Component\\ Lock\\ LockInterface::acquireRead ` method
181
184
to acquire a read-only lock, and the existing
182
185
:method: `Symfony\\ Component\\ Lock\\ LockInterface::acquire ` method to acquire a
183
- write lock. ::
186
+ write lock::
184
187
185
188
$lock = $factory->createLock('user'.$user->id);
186
189
if (!$lock->acquireRead()) {
187
190
return;
188
191
}
189
192
190
- Similare to the ``acquire `` method, pass ``true `` as the argument of the ``acquireRead() ``
191
- method to acquire the lock in a blocking mode. ::
193
+ Similar to the ``acquire() `` method, pass ``true `` as the argument of ``acquireRead() ``
194
+ to acquire the lock in a blocking mode::
192
195
193
196
$lock = $factory->createLock('user'.$user->id);
194
197
$lock->acquireRead(true);
195
198
196
- When a read-only lock is acquired with the method ``acquireRead ``, it's
197
- possible to **Promote ** the lock, and change it to write lock, by calling the
198
- ``acquire `` method. ::
199
+ When a read-only lock is acquired with the method ``acquireRead() ``, it's
200
+ possible to **promote ** the lock, and change it to write lock, by calling the
201
+ ``acquire() `` method::
199
202
200
203
$lock = $factory->createLock('user'.$userId);
201
204
$lock->acquireRead(true);
@@ -207,13 +210,8 @@ possible to **Promote** the lock, and change it to write lock, by calling the
207
210
$lock->acquire(true); // Promote the lock to write lock
208
211
$this->update($userId);
209
212
210
- In the same way, it's possible to **Demote ** a write lock, and change it to a
211
- read-only lock by calling the ``acquireRead `` method.
212
-
213
- .. versionadded :: 5.2
214
-
215
- The ``Lock::acquireRead `` method and ``SharedLockStoreInterface `` interface
216
- and were introduced in Symfony 5.2.
213
+ In the same way, it's possible to **demote ** a write lock, and change it to a
214
+ read-only lock by calling the ``acquireRead() `` method.
217
215
218
216
The Owner of The Lock
219
217
---------------------
@@ -833,3 +831,4 @@ are still running.
833
831
.. _`PHP semaphore functions` : https://www.php.net/manual/en/book.sem.php
834
832
.. _`Replica Set Read and Write Semantics` : https://docs.mongodb.com/manual/applications/replication/
835
833
.. _`ZooKeeper` : https://zookeeper.apache.org/
834
+ .. _`readers–writer lock` : https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock
0 commit comments