petesmiley has asked for the wisdom of the Perl Monks concerning the following question:

Has anyone tried using DB_File or SDBM_File or modules of that sort with threads in perl 5.8? I keep getting a segmentation fault when my program exits.

I provided a short code sample that produces the problem on my machine. I don't think it even makes it to the untie. To get rid of the segfault all I have to do is comment out the tie.

Maybe someone could point out something I'm doing wrong, or suggest a module that works better for this. Any advice would be appreciated.

smiles

#!/usr/bin/perl use strict; use threads; use threads::shared; use Thread::Queue; use SDBM_File; use Fcntl; # initialize some stuff my $q = Thread::Queue->new(); my %items : shared; tie %items,'SDBM_File','itemdb',O_CREAT|O_RDWR,0640 or die "Could not +open SDBM file: $!\n"; $items{$_} = 1 for 1..10; # play with 3 threads for (1..3) { threads->new(\&ttest) } my @threadlist = threads->list; $q->enqueue($_) for 1..10; $q->enqueue(undef) for @threadlist; $_->join for @threadlist; # clean up mess untie %items; unlink 'itemdb.pag','itemdb.dir'; sub ttest { while (my $qthingie = $q->dequeue) { print "$items{$qthingie}\n" } }

Replies are listed 'Best First'.
Re: threads, tie and segmentation fault
by pg (Canon) on Feb 04, 2003 at 20:49 UTC
      I agree, and thanks. I look forward to reading it.
      That still doesn't quite help with my current quandary though. :)
Re: threads, tie and segmentation fault
by petesmiley (Friar) on Feb 05, 2003 at 17:40 UTC
    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; } }
Re: threads, tie and segmentation fault
by perrin (Chancellor) on Feb 04, 2003 at 17:51 UTC
    Those are XS modules. Did someone tell you they were thread safe? I assume they are not.
      I kind of figured :) But is there a nice alternative to these?