There is a script, I want to make it multi-threaded.
Suppose there is a certain array of hashes containing
my %hello : shared; print Dumper(\%hello); $VAR1 = { 'a' => '1', 'b' => '2', 'c' => '3', 'e' => '4', 'f' => '5', 'g' => '6' }; And a certain function which, let's say, just print'om displays the elements of this array on the screen.
What I want: I want to run it somehow in, say, 2 threads. I see it this way: Since the array is shared, we run in an infinite loop until something in the array is our function that simply “takes” the value from it and does something with it.
The question is: how can this be explained to the machine on perl and will it work at all? :)
UPD
Something like that, just so that the streams do not do the same actions (Now they just in turn output all the contents of the hashes array completely):
#!/usr/bin/perl use threads; use threads::shared; my $threads = 2; my @threads; my %hello : shared; sub test { my $num=shift; print "+ Thread $num started.\n"; foreach my $key (keys %hello) { print $key, $hello{$key} . "\n"; } } for my $t (1..$threads) { push @threads, threads->create(\&test, $t); } # Дожидаемся окончания работы всех потоков foreach my $t (@threads) { $t->join(); } exit(0);
delete($hello{$key})functiondelete($hello{$key}), the first thread does all the work, only somehow “crookedly”, sometimes stitching together lines and, in my opinion, duplicating them ... apparently I don’t understand something, did not take into account something. - user226699system("/usr/bin/nmap -p80 -PN -n -T5 --defeat-rst-ratelimit --nsock-engine select --open --min-parallelism 254 -oG - $ip| awk '/open/{print \$2}' >> $path");functionsystem("/usr/bin/nmap -p80 -PN -n -T5 --defeat-rst-ratelimit --nsock-engine select --open --min-parallelism 254 -oG - $ip| awk '/open/{print \$2}' >> $path");- user226699