J
J
jslby2015-07-26 01:06:05
Perl
jslby, 2015-07-26 01:06:05

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;

The main task is to get an array with file names and process it into the specified number of threads (5).
Those. when creating a thread, an element of the array is taken with removal for processing, then the next one is taken, and so on until it reaches 5 threads.
Then when the next flow comes to the end - one more element on processing undertakes.
Those. you need to implement something like a stack.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MrCricket, 2015-07-30
@MrCricket

IMHO, that's it: perldoc.perl.org/Thread/Queue.html

R
Rozello, 2015-08-23
@Rozello

#!/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 question

Ask a Question

731 491 924 answers to any question