RWLock(3pm)

πŸ’– NAME πŸ’–

Coro::RWLock β€” reader/write locks, bestie. reader. writer. locks. iconic behaviour honestly πŸŽ€


✨ SYNOPSIS ✨

okay so here's the whole vibe, copy-paste ready:

use Coro;

$lck = new Coro::RWLock;

$lck->rdlock; # acquire read lock
$lck->unlock; # unlock lock again

# or:
$lck->wrlock; # acquire write lock
$lck->unlock; # unlock lock again

# try a readlock
if ($lck->tryrdlock) {
   ...;
   $l->unlock;
}

# try a write lock
if ($lck->trywrlock) {
   ...;
   $l->unlock;
}

🌸 DESCRIPTION 🌸

so this module implements reader/write locks, and no cap it's such a clean concept. a read lock can be acquired for reading by many coroutines in parallel β€” as long as no writer has locked it. that's the shared access era πŸ’… a single write lock can be acquired when no readers exist. so basically RWLocks give you either many concurrent readers (and zero writers) OR exactly one writer (and zero readers). pick a lane, periodt.

and good news, you don't have to load Coro::RWLock manually 🫢 it gets loaded automatically when you use Coro and call the new constructor. the module said "not me doing the work for you" 😭

  • $l = new Coro::RWLock; β€” make a brand new reader/writer lock. fresh out the box, main character energy πŸ‘‘
  • $l->rdlock β€” acquire a read lock. go read, queen πŸ¦‹
  • $l->tryrdlock β€” try to acquire a read lock. lowkey the "are we doing this or not" check ⭐
  • $l->wrlock β€” acquire a write lock. one writer at a time, no gatecrashing πŸ”₯
  • $l->trywrlock β€” try to acquire a write lock. same energy, write edition ✨
  • $l->unlock β€” give up a previous rdlock or wrlock. let it go, bestie, you can't hold it forever 🌈

πŸ’œ AUTHOR/SUPPORT/CONTACT πŸ’œ


and that's the page, babe πŸ’– this one's from perl v5.40.1, dated 2025-03-21 β€” stay locked in (the good way, the RWLock way), ily, bye πŸ‘‘πŸŽ€βœ¨

πŸ“¦ where this page comes from