I need to test single IPs against listed CIDRs (a small (<10) set)
Personally, on the basis of that description--ie.readonly use of the object once initally constructed--I wouldn't try to share the object.
Simply create the object before you create your threads and allow it to be cloned into the threads, either passed as an argument or through closure. Each thread will then have it's own (identical) copy of the object and can go ahead with the comparisons at full speed without any need for locking.
This seems to run perfectly:
#! perl -slw use strict; use Math::Random::MT qw[ rand ]; use Net::CIDR::Lite; use threads; our $WORKERS ||= 10; sub n2ip { join '.', unpack 'C4', pack 'V', $_[ 0 ] } sub worker { my $tid = threads->tid; my $CIDR = shift; for( 1 .. 1000 ) { my $ip = n2ip( int( rand 2**32 ) ); printf "[$tid] $ip was %s\n", $CIDR->find( $ip ) ? 'found' : 'not found'; } } my $CIDR = Net::CIDR::Lite->new( map{ n2ip( int( rand 2**32 ) ) . '/' . ( 1+int( rand 32 ) ) } 1 .. 10 ); print for $CIDR->list; printf "Enter to run threads"; <>; my @threads = map{ threads->create( \&worker, $CIDR ); } 1 .. $WORKERS; $_->join for @threads; __END__ C:\test>CIDR -WORKERS=100 16.0.0.0/4 133.105.224.0/20 155.64.0.0/10 191.83.33.64/26 192.0.0.0/3 224.0.0.0/4 Enter to run threads [1] 133.253.94.106 was not foun [1] 142.170.146.37 was not foun [1] 255.246.52.63 was not found [1] 223.1.163.7 was found [2] 93.38.214.136 was not found [3] 74.172.118.234 was not foun [3] 205.199.108.66 was found [3] 124.251.49.2 was not found [3] 230.87.252.108 was found [3] 195.216.205.202 was found [3] 240.11.9.134 was not found [3] 234.100.16.237 was found [3] 210.137.186.2 was found
If you needed to add new IPs or ranges to the object on the fly, then I'd use a client-server arrangement coordinated through a queue and a shared hash...but that doesn't seem to be necessary for this case.
In reply to Re: Is a Net::CIDR::Lite object sharable within threads
by BrowserUk
in thread Is a Net::CIDR::Lite object sharable within threads
by Wiggins
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |