Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Reaped: beginner:pls finished the multiple thread port scanner scripts

by 7stud (Deacon)
on Feb 18, 2013 at 19:27 UTC ( #1019388=note: print w/ replies, xml ) Need Help??


in reply to Reaped: beginner:pls finished the multiple thread port scanner scripts

1)

use strict; use warnings; use 5.012; use threads; use Thread::Queue; my $q = Thread::Queue->new; sub do_stuff { my $id = threads->tid; my $data; while ($data = $q->dequeue) { #Process data: sleep rand(5); say "$id: Done with: '$data'. ", 'Going back to q to get more data.'; } say "$id: No more data in q. Quitting..."; } my $max_threads = 5; my @threads = map { threads->create(\&do_stuff) } 1 .. $max_threads ; #All threads are now blocking on dequeue() and waiting for data. my @data = qw( hi hello greetings what where how world earth gia mars venus jupiter ); for my $data (@data) { $q->enqueue($data); } #Make all threads exit their while loops: for (1 .. $max_threads) { $q->enqueue(undef); } #Don't end the program until all threads have finished: for my $thr (@threads) { $thr->join } --output:-- 1: Done with: 'greetings'. Going back to q to get more data. 5: Done with: 'hello'. Going back to q to get more data. 3: Done with: 'what'. Going back to q to get more data. 5: Done with: 'world'. Going back to q to get more data. 5: Done with: 'earth'. Going back to q to get more data. 4: Done with: 'how'. Going back to q to get more data. 2: Done with: 'hi'. Going back to q to get more data. 1: Done with: 'where'. Going back to q to get more data. 1: No more data in q. Quitting... 5: Done with: 'mars'. Going back to q to get more data. 5: No more data in q. Quitting... 3: Done with: 'gia'. Going back to q to get more data. 3: No more data in q. Quitting... 4: Done with: 'venus'. Going back to q to get more data. 4: No more data in q. Quitting... 2: Done with: 'jupiter'. Going back to q to get more data. 2: No more data in q. Quitting...


Comment on Re: Reaped: beginner:pls finished the multiple thread port scanner scripts
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1019388]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2015-07-06 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (69 votes), past polls