Skip to content

Instantly share code, notes, and snippets.

@pdl
Created January 2, 2014 17:35
Show Gist options
  • Save pdl/8222887 to your computer and use it in GitHub Desktop.
Save pdl/8222887 to your computer and use it in GitHub Desktop.
Using Threads and XML::LibXML appears to cause a memory allocation bug.
use strict;
use warnings;
use threads;
use Thread::Queue;
my $q = Thread::Queue->new(); # A new empty queue
my $q2 = Thread::Queue->new(); # A new empty queue
use XML::LibXML qw(:threads_shared);
my $parser = XML::LibXML->new;
my @docs =
map { $parser->parse_string(qq{<root><foo id="$_">bar</foo></root>}) }
1..10
;
# Worker threads
my $getter = threads->create(
sub {
# Thread will loop until no more work
while (defined(my $item = $q->dequeue())) {
# $q2->enqueue($item);
$q2->enqueue( $item->documentElement->findvalue('//foo/@id'));
}
$q2->enqueue (undef);
}
);
my $printer = threads->create(
sub {
# Thread will loop until no more work
while (defined(my $item = $q2->dequeue())) {
print "\n".$item;
}
}
);
$q->enqueue(@docs);
$q->enqueue(undef);
$printer->join();
$getter->join();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment