Skip to content

Instantly share code, notes, and snippets.

@fpl
Created November 20, 2023 08:59
Show Gist options
  • Save fpl/2eb2bbd13999e52f9e9ce228c45c117a to your computer and use it in GitHub Desktop.
Save fpl/2eb2bbd13999e52f9e9ce228c45c117a to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use v5.18;
use threads;
use warnings;
use strict;
use Thread::Queue;
use Geo::GDAL::FFI;
my $q = Thread::Queue->new();
my @in_thrds = ();
my @out_thrds = ();
my $nt = 5;
for my $i (1..$nt) {
my $t = threads->create(
sub {
while (my $h = $q->dequeue()) {
say "thread out$i: popped $h->{value}";
}
}
);
push @out_thrds, $t;
}
for my $i (1..$nt) {
my $t = threads->create(
sub {
my $v = rand(100);
say "thread in$i: pushed $v";
$q->enqueue({ value => $v });
}
);
push @in_thrds, $t;
}
# try different timing too... :-/
sleep(5);
$q->end();
for my $w (@in_thrds) {
$w->join();
}
for my $w (@out_thrds) {
$w->join();
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment