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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.