Thread::Resource::RWLock

Read/write lock base class for Perl ithreads
Download

Thread::Resource::RWLock Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Dean Arnold
  • Publisher web site:
  • http://search.cpan.org/~darnold/

Thread::Resource::RWLock Tags


Thread::Resource::RWLock Description

Read/write lock base class for Perl ithreads Thread::Resource::RWLock is a Perl module that provides both an inheritable abstract class, as well as a concrete object implementation, to regulate concurrent access to resources. Multiple concurrent reader threads may hold a Thread::Resource::RWLock readlock at the same time, while a single writer thread holds the lock exclusively.New reader threads are blocked if any writer is currently waiting to obtain the lock. The read lock is granted after all pending write lock requests have been released.SYNOPSIS package LockedObject; use threads; use threads::shared; use Thread::Queue::Queueable; use Thread::Resource::RWLock; use base qw(Thread::Queue::Queueable Thread::Resource::RWLock); sub new { my $class = shift; my %obj : shared = (); my $self = bless \%obj, $class; # # init the locking members # $self->Thread::Resource::RWLock::adorn(); return $self; } sub redeem { my ($class, $self); return bless $self, $class; } package main; use threads; use threads::shared; use Thread::Queue::Duplex; use LockedObject; # # in threaded app: # my $read_write = LockedObject->new(); my $tqd = Thread::Queue::Duplex->new(); my $thrdA = threads->new(\&read_thread, $tqd); my $thrdB = threads->new(\&write_thread, $tqd); # # pass the shared object to each thread # $tqd->enqueue_and_wait($read_write); $tqd->enqueue_and_wait($read_write); # Reader sub read_thread { my $tqd = shift; my $request = $tqd->dequeue(); $tqd->respond($request->, 1); my $obj = $request->; my $locktoken = $obj->read_lock(); # # do some stuff # $obj->unlock($locktoken); } # Writer sub write_thread { my $tqd = shift; my $request = $tqd->dequeue(); $tqd->respond($request->, 1); my $obj = $request->; # # first grab a readlock # my $locktoken = $obj->read_lock(); # # do some stuff, then upgrade to a writelock # $obj->write_lock(); # # do some stuff, then unlock # $obj->unlock($locktoken); } Requirements: · Perl


Thread::Resource::RWLock Related Software