This isn't much of a meditation. More of a, here's a possibility that I do not have the time to pursue, does anyone want to pick it up, play with, run with it or shoot it down in flames.

It would appear from the discussion and posts here that people want to share objects across threads. That currently isn't possible without extreme shenanigans because shared variables are implemented using tie magic. And blessing object references also uses magic. And you cannot apply the two types of magic to a single entity.

More or less. There maybe more to it than that, but it will take someone with a better understanding of the internals than I have to explain more.

There are existing solutions to this problem that involve creating proxy objects that communicate all the actions to a master object. The problem with this is that they are incredibly slow because every method call requires at least 2 task switches and usually more.

The basis of my proposed solution is a little known perl OO technique termed (by dominus) Globjects. Follow that link to read about them, I'll stick to how they permit the sharing of objects across threads here.

Globjects are basically blessed globs. As you may know, a glob is an internal container which can hold, amongst other things a scalar, an array and a hash. See broquaint's excellent description Of Symbol Tables and Globs for more on them.

By basing objects around a blessed glob, you can share the scalar, array or hash, (or all 3 if you wish), that the glob contains. And because the you don't need to share the glob itself, the glob can be passed around and cloned, whilst retaining is blessedness, and allow the methods called via the blessing magic, to access the shared elements it contains. And that is pretty much it.

To get anyone with the time and motivation going, here is my extremely simple and un-thorough proof of concept code:

#! perl -slw package dummy; use threads; use threads::shared; sub new{ my $self = bless do{ local *GLOB; \*GLOB }, $_[0]; share( %{ *$self } ); %{ *$self } = qw[ there are some parameters here 1 ]; $self; } sub get{ my $self = shift; @{ *$self }{ @_ || sort keys %{ *$self } } ; } sub put{ my $self = shift; lock %{ *$self }; ${ *$self }{ +shift } = shift while @_; } package main; use strict; use threads; use threads::shared; our $N ||= 10; sub t{ my $obj = shift; sleep 1; my $tid = threads->self->tid; warn "$tid: ", join'|', $obj->get(), $/; sleep rand 10; $obj->put( $tid, $tid ); warn "$tid: ", join'|', $obj->get(), $/; } my $obj = new dummy; print join '|', $obj->get; my @t = map{ threads->create( \&t, $obj ) } 1 .. $N; $_->join for @t; print join '|', $obj->get; __END__ c:\test>sglobjects.pl 1|parameters|are 1: 1|parameters|are| 2: 1|parameters|are| 3: 1|parameters|are| 3: 3|1|parameters|are| 4: 3|1|parameters|are| 5: 3|1|parameters|are| 6: 3|1|parameters|are| 7: 3|1|parameters|are| 8: 3|1|parameters|are| 9: 3|1|parameters|are| 10: 3|1|parameters|are| 1: 1|3|1|parameters|are| 7: 1|3|7|1|parameters|are| 4: 1|3|4|7|1|parameters|are| 9: 1|3|4|7|9|1|parameters|are| 2: 1|2|3|4|7|9|1|parameters|are| 5: 1|2|3|4|5|7|9|1|parameters|are| 6: 1|2|3|4|5|6|7|9|1|parameters|are| 10: 1|10|2|3|4|5|6|7|9|1|parameters|are| 8: 1|10|2|3|4|5|6|7|8|9|1|parameters|are| 1|10|2|3|4|5|6|7|8|9|1|parameters|are

What that output shows is that all 10 threads are able to indirectly manipulate (via methods) the contents of the shared hash contained within the blessed glob. The same also works for the array and scalar it contains. And that's all it shows. Everything else, destruction, performance etc. needs testing and proving. Enjoy.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Shareable objects. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.