Don't ask to ask, just ask | |
PerlMonks |
Shareable objects.by BrowserUk (Patriarch) |
on Sep 05, 2007 at 09:00 UTC ( [id://637089]=perlmeditation: print w/replies, xml ) | Need Help?? |
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:
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.
Back to
Meditations
|
|