Christopher Fowler wrote:
I think I'm understanding things correctly.  I've started on my second
exercise and have working code.  Here is the basics of the program

1.  It does a ping heartbeat on various hosts
2.  A piece of data is shared among threads.
3.  There is a thread for each target.
4.  There is one thread that will notify when a host goes down.

I have a few questions.

     67   # Read out config file:
     68   my $xml = XMLin "config.xml";

XMLIn returns a reference to a hash.  How can I share that data?
Currently I'm creating data from that data.

     33 my %data;
     34 my $lock;
     35 share($lock);
     36 share(%data);
     ....
     74   {
     75     lock($lock);
     76     foreach my $ref (keys %{$xml->{'host'}}) {
77 78 # We need to share this child of the hash and this
     79       # Child's elements
     80       $data{$ref} = &share({});
81 82 $data{$ref}{'ip'} = $xml->{'host'}{$ref}{'ip'};
     83       $data{$ref}{'last_ping'} = $xml-
{'host'}{$ref}{'last_ping'};
     84       $data{$ref}{'counter'} = 0;
     85       $data{$ref}{'up'} = 0;
     86       $data{$ref}{'interval'} = $xml-
{'host'}{$ref}{'interval'};
87 88 print "Creating thread to monitor: $data{$ref}{'ip'}\n"; 89 90 # Fire off the thread.
     91       threads->create(\&worker_bee, $ref);
     92     }
     93   }

For each level of a hash do I have to share it? 80 $data{$ref} = &share({});
Without that the programed died because of invalid value applied to
shared variable.
How deep can the hash go and still be shared?

Not very. Every ref assigned to a shared variable (scalar, array element, or
hash element) must also be thread::share'd. Which (recursively) includes the
children of said shared variable.

Do I need to do that for each level if I expect many branches?

Depends. I haven't looked at your program (as I'm not paid to do so ;^),
but if your only purpose is to send the XML from thread A to thread B,
you'll probably want to use Storable freeze()/thaw(). Alas, that may
be computationally expensive...at which point you may need
to throw some XS/C at the problem. Or perhaps do some simple XML 
parsing/extracting
in your app and just send the inner XML elements as XML to the other threads 
and have
them deconstruct it...YMMV.

I suspect you're at the point at which app requirements will determine the best
approach.

And, as noted in my prior posts, using multilevel deref's of shared hashrefs
seems to have some issues, so you may need to incrementally assign to lexicals
as you traverse the XMLin structure.

Good luck!

- Dean

Reply via email to