Multithreading – Perl threads: how to make a producer?

I have a feature that runs very slowly I need to enter this function in the main part of the program So I want to do something similar to UNIX commands, which produce as much input as possible, but only a little more Unlike that, I don't want the value of stdin, but I want the value in the Perl queue

In other words: the problem is not about the selection of file handles, but about the queue maintained by threads

I think the metacode looks like this:

my $DataQueue = Thread::Queue->new();

my @producers;
my $no_of_threads = 10;
for (1..$no_of_threads) {
  push @producers,threads->create(\&producer);
}

for(<>) {
  # This should block until there is a value to dequeue
  # Maybe dequeue blocks by default - then this part is not a problem
  my $val = $DataQueue->dequeue();
  do_something($_,$val);
}
# We are done: The producers are no longer needed
kill @producers;

sub producer {
   while(1) {
      # How do I wait until the queue length is smaller than number of threads?
      wait_until(length of $DataQueue < $no_of_threads);
      $DataQueue->enqueue(compute_slow_value());
   }
}

But is there a more elegant way? I'm particularly unsure how to execute wait in an effective way_ Until section

Solution

Something like this might work:

my $DataQueue = Thread::Queue->new();

my @producers;
my $no_of_threads = 10;
for (1..$no_of_threads) {
  push @producers,threads->create(\&producer);
}
$DataQueue->limit = 2 * $no_of_threads;

for(<>) {
  # This blocks until $DataQueue->pending > 0
  my $val = $DataQueue->dequeue();
  do_something($_,$val);
}
# We are done: The producers are no longer needed
kill @producers;

sub producer {
   while(1) {
      # enqueue will block until $DataQueue->pending < $DataQueue->limit
      $DataQueue->enqueue(compute_slow_value());
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>