Answer the question
In order to leave comments, you need to log in
How to organize a queue of threads?
It’s already late, I’m a little dull, and even when you don’t know ... it’s generally difficult. Please tell me how to organize a queue of threads for processing files:
#!/usr/bin/perl -w --
use strict;
use warnings;
package main;
use threads;
use threads::shared;
my @files = qw(1111.txt 2222.txt 3333.txt 4444.txt 5555.txt 6666.txt 7777.txt 8888.txt 9999.txt 0000.txt);
sub getfile {
my $file = pop(@files);
print threads->tid(), ': ', $file, "\n";
threads->yield();
}
sub main {
my @files :shared;
my @t;
for (0 .. scalar(@files) / 5) {
push(@t, threads->create( sub { getfile() } ));
}
$_->join foreach (@t);
threads->exit();
}
&main;
1;
Answer the question
In order to leave comments, you need to log in
#!/usr/bin/env perl
use strict;
use warnings;
use threads;
use threads::shared;
# Создаём расшареную переменную
my @numbers:shared = (1..100);
# Задаём количество потоков
my $threads = shift || 10;
# Создаём потоки и кладём их обекты в массив
my @threads;
for (1..$threads) {
push @threads, threads->new(
sub {
while (@numbers) {
# Достаём данные из массива
# Предварительно заблокировава его для остальных потоков
my $number;
{
# Блокировка работает только в этом скоупе
lock(@numbers);
$number = shift(@numbers);
}
print 'Result: '.($number*10)."\n";
}
}
);
}
# Запускаем потоки привязав их к основному процессу
$_->join for (@threads);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question