Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Thread-safe singleton in perl

by Saladino (Beadle)
on Mar 19, 2008 at 18:48 UTC ( [id://675062]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://675062]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found