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

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

Thread Previous | Thread Next
From:
Tom Molesworth via perl5-porters
Date:
April 10, 2018 14:27
Subject:
Re: [perl #133021] Removed the word "discouraged" from threads'documentation
Message ID:
CAGXhHd=VY-D=Pp-RwS4CUeyMOox5hk+hsT34nHzTibD-Ok11FA@mail.gmail.com
On 10 April 2018 at 21:59, Dave Mitchell <davem@iabyn.com> 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:

#!/usr/bin/perl
use strict;
use warnings;

use threads;
use threads::shared;

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

my $t = threads->create(sub {
    lock $lockvar;
    cond_wait($lockvar);
    my @data : shared = qw(example content here);
    $items{0 + \@data} = \@data;
});
my $seen_item;
while(1) {
    do {
        lock $lockvar;
        cond_broadcast($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;
}
$t->join;

=pod

Sample output (5.26.1):

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

=cut

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About