On Tue, Apr 20, 2004 at 04:14:26PM -0400, hx wrote:
> hi perl-loop. i'm attempting to use Event, ithreads, and shared
> variables together. perl is 5.8.3, Event is 0.87. in particular, i'd
> like to create a watcher that watches a shared variable. sample code:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> use threads;
> use threads::shared;
>
> my $data : shared = 0; # the shared variable
>
> #BEGIN {
> my $writer = new threads ( sub {
> while (1) {
> $data++;
> print "write: $data (ref is " . \$data . ")\n";
> sleep (2);
> } } );
> #} # end of BEGIN block
>
> use Event;
>
> Event->var (var => \$data,
> poll => "w",
> cb => sub { print "read: $data (ref is " . \$data . ")\n"; } )
> or die "can't register variable watcher";
>
> Event->timer ( interval => 2,
> cb => sub { print "timer: $data (ref is " . \$data . ")\n"; } )
> or die "can't register timer";
>
> Event::loop ();
>
> ... and some output:
>
> [561] [EMAIL PROTECTED] ~ $ ./foo.pl
> write: 1 (ref is SCALAR(0x102b0220))
> timer: 1 (ref is SCALAR(0x1012bcf8))
> write: 2 (ref is SCALAR(0x102b0220))
> timer: 2 (ref is SCALAR(0x1012bcf8))
> write: 3 (ref is SCALAR(0x102b0220))
> timer: 3 (ref is SCALAR(0x1012bcf8))
> ...
>
> the thing to notice is that the variable watcher never triggers. i
> guess that might make sense because the references are different.
> (that's a guess since i don't really know how var watchers are
> implemented.)No, that's not the problem. Those pointers probably refer to the same variable. The problem is that Event uses 'U' magic to subscribe to notifications of variable reads/writes. Unsurprisingly, 'U' magic isn't automatically propagated through a thread-safe queue. There is a limit to DWIMery! > how can one create a watcher that watches a shared variable? Event is not designed to run in more than one thread. Use a thread-safe queue to communicate between the thread running Event and other threads. -- A new cognitive theory of emotion, http://openheartlogic.org
signature.asc
Description: Digital signature
