in reply to Re^3: Shared variables between threads
in thread Shared variables between threads

I have updated my threads and threads::shared packages but no way.
threads version -> 1.67 threads::shared version -> 1.14 Invalid value for shared scalar at RTP_media.pl line 23.
lines 22-23
my $capobj:shared; $capobj = Net::Pcap::open_live($dev,1024,0,0,\$err);

Replies are listed 'Best First'.
Re^5: Shared variables between threads
by BrowserUk (Patriarch) on Nov 05, 2007 at 11:00 UTC

    You will need to threads::shared::share it before you'll be able to assign it to a shared variable.

    #! perl -slw use strict; use threads; use threads::shared; print "threads: $threads::VERSION threads::shared:$threads::shared::VE +RSION"; { my $scalar = 0xdeadbeef; my $blessed = bless \$scalar, 'main'; print 'Scalar: ', $blessed; my $shared :shared = &share( $blessed ); print 'Scalar: ', $shared; } __END__ C:\test>t-blessed.pl threads: 1.67 threads::shared:1.14 Scalar: main=SCALAR(0x1824624) Scalar: main=SCALAR(0x1824624)

    For your case that should look something like:

    my $capobj:shared; $capobj = &share( Net::Pcap::open_live($dev,1024,0,0,\$err) );

    Or just

    my $capobj:shared = &share( Net::Pcap::open_live($dev,1024,0,0,\$err) +);

    The ampasand on &share() is necessary to defeat the prototype.


    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.
      Am I doing something wrong? Here is a full script and its output.
      #!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; use Net::Pcap; my $err; my $dev = Net::Pcap::lookupdev(\&err); my ($address,$netmask); Net::Pcap::lookupnet($dev,\$address,\$netmask,\$err); my $filter; print "threads version -> ".$threads::VERSION." threads::shared versio +n -> ".$threads::shared::VERSION."\n"; my $capobj:shared = &share(Net::Pcap::open_live($dev,1024,0,0,\$err)) +; print "shared $capobj\n"; my $capthread = threads->new(\&capture); sleep 5; Net::Pcap::breakloop($capobj); print "$capobj breakloop\n"; $capthread->join(); print "Capture thread finalized\n"; sub capture { print "capture->$capobj\n"; Net::Pcap::compile($capobj,\$filter,'host XX.XX.XX.XX && port +7777 && udp',1,$netmask); Net::Pcap::setfilter($capobj,$filter); Net::Pcap::loop($capobj,0,\&capture_callback, 0); print "Outside the loop\n"; Net::Pcap::close($capobj); } sub capture_callback { }
      And the output:
      $perl test3.pl threads version -> 1.67 threads::shared version -> 1.14 shared pcap_tPtr=SCALAR(0x8ee08f8) capture->pcap_tPtr=SCALAR(0x8f223b4)

        You show some output, but you don't say what else happened. Did it segfault (trap)? Hang? Just terminate silently?

        I've never used Pcap, so anything I say at this point is a guess, but a few things stand out in your code.

        1. You are passing a loop count of 0 to loop().

          The docs say:

          Read $count packets from the packet capture descriptor $pcap and call the perl function &callback with an argument of $user_data. If $count is negative, then the function loops forever or until an error occurs. Returns 0 if $count is exhausted, -1 on error, and -2 if the loop terminated due to a call to pcap_breakloop() before any packets were processed.

          Which suggest to me that if you want to capture 5 seconds of input and then break the loop, you should be passing -1 for the loop count.

        2. Your callback_capture() sub is empty.

          You probably ought to try adding a print statement in there to see a) if you are being call back and b) are you being passed anything.

        3. You should definitely be checking (printing) the returns from the Pcap subs--they might be very informative.

          I'd suggest using warn instead fo print for your debug and trace messages as stderr is unbuffered and you may not be seeing stuff that has been printed to stdout but not yet flushed.


        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.