Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

New in Symfony 5.2: Shared locks

Contributed by
Jérémy Derussé
in #37752.

In computer science, the readers–writers problems deal with situations in which many concurrent threads of execution try to access the same shared resource at one time.

A readers–writer lock is a synchronization primitive that solves one of those problems. It allows concurrent access for read-only operations, while write operations require exclusive access. This means that multiple threads can read the data in parallel but an exclusive lock is needed for writing or modifying data.

In Symfony 5.2 we’ve added support for them thanks to shared locks. When the lock store implements the new SharedLockStoreInterface, you can call the acquireRead() method to get a read-only lock, while the existing acquire() method gets a write lock:

1
2
3
4
$lock = $factory->createLock('user'.$user->id);
if ($lock->acquireRead()) {
    // ...
}

Read the new docs about shared locks to learn all about them.

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

Comments

I think documentation (and probably this article too) should include explanation what bias is implemented: towards readers or towards writers.
@Ivan good question! We're clarifying this in the docs: https://github.com/symfony/symfony-docs/pull/14460
Login with SymfonyConnect to post a comment