Now that I have a slightly clearer understanding of how perl threads work, I have a piece of code that resolves this problem. Note that tying and untying with locks solves the problem. Apparently a tie will not carry well when spawning threads so each one would have to do its own tying and untying.
#!/usr/bin/perl
use strict;
use threads;
use threads::shared;
use Thread::Queue;
use DB_File::Lock;
use Fcntl;
my $q = Thread::Queue->new();
my %items;
my $DB = tie %items,'DB_File::Lock','itemdb',O_CREAT|O_RDWR,0640,$DB_H
+ASH,'write' or die "Could not open SDBM file: $!\n";
$items{$_} = 1 for 1..10;
untie %items;
undef $DB;
for (1..3) { threads->new(\&ttest) }
my @threadlist = threads->list;
$q->enqueue($_) for 1..10;
$q->enqueue(undef) for @threadlist;
$_->join for @threadlist;
unlink 'itemdb.pag','itemdb.dir';
sub ttest {
my %db;
while (my $qthingie = $q->dequeue) {
my %items;
my $DB = tie %items,'DB_File::Lock','itemdb',O_CREAT|O
+_RDWR,0640,$DB_HASH,'write' or die "Could not open SDBM file: $!\n";
print "$qthingie $items{$qthingie}\n";
untie %items;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.