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

Hi monks, I'd like to know if it is technically possible to have a thread-safe singleton in perl 5.8.8. I mean, my problem now is that I've to share the object reference and threads::shared doesn't allow that. I've been looking this url: http://perlmonks.org/?node_id=237710 But at this moment it segfaults in my Debians perl 5.8.8 . Any hints?

Replies are listed 'Best First'.
Re: Thread-safe singleton in perl
by pc88mxer (Vicar) on Mar 19, 2008 at 19:01 UTC
    It also segfaults on my Ubuntu box. However, changing
    my @threads = threads->new( \&worker, \$_ ) for @flags;
    to:
    my @threads; for my $i (0..$#flags) { push(@threads, threads->new(\&worker, \$flags[$i]); }
    makes the example work for me.
Re: Thread-safe singleton in perl
by renodino (Curate) on Mar 19, 2008 at 19:26 UTC
    my problem now is that I've to share the object reference and threads::shared doesn't allow that.

    Care to elaborate on that ? I've certainly used shared objects on numerous occasions. (There is a small gotcha related to DESTROY that has a fix in perl 5.10 + latest CPAN version of threads::shared).

    Presumably, you simply need to bless a shared variable for the object. See threads::shared Objects description for details.


    Perl Contrarian & SQL fanboy