in reply to Shared variables between threads

It is not clear why you need the pcap object to be SHARED, since your code shows no locking around it anyway, and only one thread appears to be active.

My suggestion would be to create the my $capobj = Net::Pcap::open_live($dev,1024,0,0,\$err); as a not-explicitly-shared object in the main code, then start the &capture thread - either passing it the object, or letting it access the global (thread copy of the) object.

You still retain active control in the main code, and, presumably, can still interrupt the capture by calling "breakloop", since you retain the (original) $capobj.

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Replies are listed 'Best First'.
Re^2: Shared variables between threads
by Saladino (Beadle) on Nov 02, 2007 at 16:25 UTC
    Well, The posted version is a simpler than the original, i do things at the main thread. In fact that is the key, because i need to finalize the capture thread just after finishing those things (changed by the "sleep 3" in the example).
    If i understand correctly the thread system in perl all not explicitly shared variables are copied in the child thread.
    So, if i open_live before splitting my child will obtain a copy of that descriptor. I don't know how can that affect to my script (closing or using a copied descriptor).
    I've tried it and breakloop should force the loop exit but instead it does nothing because breakloop returns correctly but loop inside the child thread never returns.