develooper Front page | perl.perl5.porters | Postings from April 2018

Re: [perl #133021] Removed the word "discouraged" from threads'documentation

Thread Previous | Thread Next
Tom Molesworth via perl5-porters
April 10, 2018 14:27
Re: [perl #133021] Removed the word "discouraged" from threads'documentation
Message ID:
On 10 April 2018 at 21:59, Dave Mitchell <> wrote:

> On Tue, Apr 10, 2018 at 02:59:50PM +0200, Christian Walde wrote:
> > If you really look at all 3 points i made, in aggregate, and don't see
> > how this is a problem and a danger, then i don't think i can come up
> > with other word combinations to make you see it.
> Can you come up with a hypothetical scenario, e.g.  a multi-threaded
> program that uses libraries to connect to a database and retrieve and
> parse some XML data. Then got through it step by step so that I can see
> why using perl and CPAN is dangerous, but using (e.g.) java and a DB and
> XML library is safe? What is the crucial difference between the two that
> flips it from being safe to unsafe?

Here's one trivial hacked-together example of code which I would argue
leads to "unexpected" results, at least from the perspective of a C or Java
programmer experienced in the ways of threads:

use strict;
use warnings;

use threads;
use threads::shared;

my %items : shared;
my $lockvar : shared;

my $t = threads->create(sub {
    lock $lockvar;
    my @data : shared = qw(example content here);
    $items{0 + \@data} = \@data;
my $seen_item;
while(1) {
    do {
        lock $lockvar;
    } until keys %items;
    print "Item with address " . $_ . " actually refers to " . (0 +
$items{0 + $_}) . "\n" for keys %items;
    last if defined $seen_item and $seen_item ne (values %items)[0];
    ($seen_item) = values %items;


Sample output (5.26.1):

    Item with address 31700632 actually refers to 31360376
    Item with address 31700632 actually refers to 30235168


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About