Re: locking with shared variables

[email protected] (Mike Pomraning)
Newsgroups perl.ithreads
Message-ID <[email protected]>
On Thu, 20 Nov 2003, Stefan Bergler wrote:

> It is sufficient to have ONE lock within ONE thread, to own the shared
> variable as long as this lock is within the scope of the thread. This
> lock is kind of inheritet by any called sub function and it does not
> make a difference if the subfunction does an additional lock or not. (I
> would rather say, the subfunctions should not do additional locking if
> the shared variable is already locked.)

It makes a difference to the design, if the subfunctions are may be called
independently:

  my $m : shared;
  sub do_this  { lock($m); $m++; }
  sub do_that  { lock($m); $m *= 2; do_this(); }

I might call "do_this" myself, or implicitly from within "do_that", and it
must hold the lock in either case.  (Along those lines, X/Open offers
PTHREAD_MUTEX_RECURSIVE and win32 has re-enterable CriticalSections, I
believe.)

Regards,
Mike
-- 
Michael J. Pomraning
[email protected]
http://pilcrow.madison.wi.us
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.