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

In my moosified TheObject package, I try to do this to share an object between threads:
around 'new' => sub { my $orig=shift; my $class=shift; my $self=$class->$orig(@_); my $shared_self : shared = shared_clone($self); return $shared_self; };
Then, in the code with threads:
my $obj :shared =new TheObject();
But then I get 'Unsupported ref type: CODE' at this line:
my $shared_self : shared = shared_clone($self);
I am running 5.8.8 with threads-1.83/threads-shared-1.37. Anyone have an idea what is going on here? Does Moose even work with threads at all?

Replies are listed 'Best First'.
Re: Trying to share a Moose object between some threads
by Tanktalus (Canon) on Aug 15, 2011 at 19:46 UTC

    From what I see in threads::shared, "Shared variables can only store scalars, refs of shared variables, or refs of shared data" ... which means you need to share every reference on down. Apparently, CODE references can't be cloned (makes sense).

    Try Coro instead of threads ;-) Does not suffer from any of the above issues. Also does not do true threads, which is how it avoids the above issues. Mind you, I've heard recently that 5.10+ is recommended for Coro.

Re: Trying to share a Moose object between some threads
by ikegami (Patriarch) on Aug 15, 2011 at 22:45 UTC
    Your problem isn't sharing a Moose object (as bad an idea as sharing objects is), it's that your object contains code references and you're trying to share those.
Re: Trying to share a Moose object between some threads
by BrowserUk (Patriarch) on Aug 15, 2011 at 20:39 UTC

    What are you hoping to gain by sharing your Moose object between threads?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.