Article View: pl.comp.lang.perl
Article #51715Re: Thread::Queue a pusty hash na wyjsciu
From: Paweł Matejski
Date: Wed, 23 Dec 2009 17:48
Date: Wed, 23 Dec 2009 17:48
45 lines
2048 bytes
2048 bytes
Krzysztof Lewicki wrote:
> Potrzebuje użyć wątków perlowych i natrafiłem na pewien problem. Napisałem
> sobie taki oto przykładowy fragment kodu:
>
> my $queue = Thread::Queue->new();
> my $t1 = threads->create(\&test_reader);
> my $t2 = threads->create(\&test_writer);
>
> $t2->join();
>
> sub test_writer
> {
> while (1)
> {
> sleep 1;
> my %t = (1 => "A", 2 => "B")
> $queue->enqueue(share(%t));
> }
> }
>
> sub test_reader
> {
> threads->detach();
>
> while (my $ref= $queue->dequeue())
> {
> my %hash = %{$ref}; # wyswietla np. HASH(0x1110970)
> print %hash; # tu juz nic nie wyswietla
> }
> }
>
> Jedna kolejka, 2 watki, jeden czyta z kolejki, drugi zapisuje. Niby
> proste. Problem pojawia sie jednak w funkcji test_reader. Otóż podczas
> wyciagania z kolejki dostaje referencje na hash (print $ref daje cos w
> stylu HASH(0x1110970)) i dotad - chyba - jest OK. Kiedy probuje wyciagnac
> hasha z referencji dostaję... pustkę.
>
> Nie wiem co z tym fantem zrobic, moze ktos mnie naprowadzi?
Mnie zazwyczaj sporo wyjaśnia:
use Dumper;
print Dumper $referencja;
--
P.M.
Technical Details
Message-ID:
<oo1a07-0m2.ln1@nixxie.mibm>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mailReferences:
<hgth64$21l6$1@news.mm.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!not-for-mail From: Paweł Matejski <madej@nospam.madej.pl.eu.org> Newsgroups: pl.comp.lang.perl Subject: Re: Thread::Queue a pusty hash na wyjsciu Date: Wed, 23 Dec 2009 18:48:40 +0100 Organization: "Portal Gazeta.pl -> http://www.gazeta.pl" Lines: 45 Message-ID: <oo1a07-0m2.ln1@nixxie.mibm> References: <hgth64$21l6$1@news.mm.pl> NNTP-Posting-Host: fw.polonia.com.pl Mime-Version: 1.0 X-Trace: inews.gazeta.pl 1261591144 16810 81.21.202.2 (23 Dec 2009 17:59:04 GMT) X-Complaints-To: usenet@agora.pl NNTP-Posting-Date: Wed, 23 Dec 2009 17:59:04 +0000 (UTC) X-User: madejp In-Reply-To: <hgth64$21l6$1@news.mm.pl> User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.9) Gecko/20080110 Thunderbird/2.0.0.9 Mnenhy/0.7.4.666 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit