🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.perl
Article #52434

threads::shared a wspóødzielenie zmiennych

#52434
From: Jarosław Lubczyński
Date: Wed, 13 Jun 2012 19:24
103 lines
2923 bytes
Środowisko:
CentOS 5.8, perl 5.8.8

Mam taki sobie kod:


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

use threads;
use threads::shared;

my $v1 : shared = 0;

my $threadsmp1 = threads->create( \&th1);
my $threadsmp2 = threads->create( \&th2);

my $v2 : shared = 0;

sub to_str
{
     my $v = shift;
     defined( $v) ? $v : 'undef!';
}

sub th1
{
     while( 1)
     {
         {
             lock( $v1);
             $v1++; $v2++;
             print( 'th1: v1=', to_str( $v1), " v2=", to_str( $v2), "\n");
         }
         sleep 1;
     }
}

sub th2
{
     while( 1)
     {
         {
             lock( $v1);
             print( 'th2: v1=', to_str( $v1), " v2=", to_str( $v2), "\n");
         }
         sleep 1;
     }
}

while( 1)
{
     sleep 1;
}

__END__


Jak widać program w jednym wątku inkrementuje zmienne współdzielone $v1 
i $v2 oraz wypisuje ich wartość poprzez procedurę to_str, w drugim tylko 
wypisuje zmienne ustawione przez pierwszy wątek.

Powinniśmy jak sądzę dostać na wyjściu coś w rodzaju:

th1: v1=1 v2=1
th2: v1=1 v2=1
th1: v1=2 v2=2
th2: v1=2 v2=2
th1: v1=3 v2=3
th2: v1=3 v2=3
..............

Tymczasem program wypisuje:

th1: v1=1 v2=1
th2: v1=1 v2=undef!
th1: v1=2 v2=2
th2: v1=2 v2=undef!
th1: v1=3 v2=3
th2: v1=3 v2=undef!
...............

Różnica jest taka, że $v2 jest zadeklarowana *po* utworzeniu obu wątków 
i w efekcie przypisania ustawione przez jeden wątek nie są widoczne w 
drugim. Czemu tak jest? Czy naprawdę wszystkie zmienne współdzielone 
muszę zadeklarować *przed* utworzeniem wątków, które z nich korzystają?

Nie doczytałem się nic na ten temat w
http://search.cpan.org/~jdhedden/threads-shared-1.40/lib/threads/shared.pm

threads 1.86, threads::shared 1.40 (najnowsze wersje pakietów)

Czego nie rozumiem?

-- 
Pozdrawiam
Jarek Lubczyński
(eljot<NULL>90125 at gmail dot com)
Nawiasy trójkątne z zawartością zbędne są

There are 10 kinds of people:
Those who understand binary and those who don't
Technical Details
Message-ID: <jrapd5$p61$1@inews.gazeta.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail
From: Jarosław Lubczyński <guess@who.com.pl>
Newsgroups: pl.comp.lang.perl
Subject: threads::shared a wspóødzielenie zmiennych
Date: Wed, 13 Jun 2012 21:24:00 +0200
Organization: "Portal Gazeta.pl -> http://www.gazeta.pl"
Lines: 104
Message-ID: <jrapd5$p61$1@inews.gazeta.pl>
NNTP-Posting-Host: apn-77-113-245-215.dynamic.gprs.plus.pl
Mime-Version: 1.0
X-Trace: inews.gazeta.pl 1339615461 25793 77.113.245.215 (13 Jun 2012 19:24:21 GMT)
X-Complaints-To: usenet@agora.pl
NNTP-Posting-Date: Wed, 13 Jun 2012 19:24:21 +0000 (UTC)
X-User: eljot_007
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit