Skip to content

Instantly share code, notes, and snippets.

@fpl
Created November 21, 2023 08:38
Show Gist options
  • Save fpl/d58bff918d10db13faeeb609e364e5c8 to your computer and use it in GitHub Desktop.
Save fpl/d58bff918d10db13faeeb609e364e5c8 to your computer and use it in GitHub Desktop.
t/threads.t for Geo::Gdal::FFI
use v5.18;
use threads;
use threads::shared;
use warnings;
use strict;
use Test::More;
BEGIN { use_ok('Geo::GDAL::FFI', qw/:all/); }
use Thread::Queue;
my $q = Thread::Queue->new();
my @in_thrds = ();
my @out_thrds = ();
my $nt = 10;
for my $i (1..$nt) {
my $t = threads->create(
sub {
my $gdal = Geo::GDAL::FFI->get_instance;
$gdal->SetErrorHandling;
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 $gdal = Geo::GDAL::FFI->get_instance;
$gdal->SetErrorHandling;
my $v = rand(100);
say "thread in$i: pushed $v";
$q->enqueue({ value => $v });
}
);
push @in_thrds, $t;
}
my $gdal = Geo::GDAL::FFI->get_instance;
$gdal->SetErrorHandling;
# try different timing too... :-/
sleep(3);
$q->end();
for my $w (@in_thrds) {
$w->join();
}
for my $w (@out_thrds) {
$w->join();
}
ok(1, "threading seems ok");
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment