Hmm...in your example there's no evidence that a lock is "taken on the subroutine itself" -- it's clear that threads 1, 2, and 3 are executing f() concurrently, which is contrary to the purpose of :locking a subroutine.

Let me rephrase -- I expected :locked subs to function like the docs indicate that the (not available under 5.8) manual lock()ing of a CODEREF (that is, lock(\&f)) used to function. That is, that invocations of ":locked" or ":locked :method" subs would be synchronized and thus serialized, rather like sync'd methods in Java.

By way of illustration, this is the moral equivalent of what I expected :locked to do:

#! /usr/bin/perl use threads; use threads::shared; $|=1; { my $l : shared; # private lexical sub f() { lock($l); # any f() in any thread is "locked" my $tid = threads->tid(); print "A}$tid}$_[0]\n"; select undef, undef, undef, rand(2); print "B}$tid}$_[0]\n"; } } # -- end scope for $l threads->create(\&f, $_) for (qw|foo bar baz|); $_->join() for threads->list(); __END__

Run it and you'll see that the threads are locking each other out. A useful device to prevent shared state corruption, very much like Java's "synchronized" keyword.

I'm having a hard time reading attributes.pm any other way.


In reply to Re: Re: (ithreads) :locked subs by Anonymous Monk
in thread (ithreads) :locked subs by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.