How brave are you?

The code below uses a technique I've been playing with to share two instances of an object between 10 threads that each modify whichever instance they get 100 times before terminating. The main thread then displays the values which will be whatever values were last set into them by whichever thread accessed them last. The program runs reliably under 5.8.4 on my (single cpu) system with no idication of errors, traps or memory leakage.

#! perl -slw use strict; use threads; use threads::shared; package Dummy; sub new { my $sem : shared; my $this : shared = 12345; my $that : shared = 'fred'; my $theOther : shared = 3.141592653; return bless { sem => \$sem, this => \$this, that => \$that, theOther => \$theOther, }, $_[ 0 ]; } sub this : lvalue { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ this } }; } sub that : lvalue { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ that } }; } sub theOther : lvalue { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ theOther } }; } sub combo { my $self = shift; lock ${ $self->{ sem } }; ${ $self->{ that } } = "[ ${ $self->{ this } } : ${ $self->{ theOt +her } } ]"; } package main; sub thread { Win32::Sleep rand( 1000 ); my( $obj ) = @_; bless $obj, 'Dummy'; for ( 1 .. 100 ) { Win32::Sleep rand( 1000 ); print threads->tid, ': ', join ' : ', $obj->this, $obj->that, + $obj->theOther; $obj->this = threads->tid; $obj->that = '' . threads->tid; $obj->theOther = rand 1000; $obj->combo; print threads->tid, ': ', join ' : ', $obj->this, $obj->that, + $obj->theOther; } } my @objs = map{ new Dummy } 1 .. 2; my @threads = map{ threads->create( \&thread, $objs[ rand 2 ] ) } 1 .. + 10; $_->join for @threads; bless $_, 'Dummy' for @objs; print 'Back in main: ', join ' :', $_->this, $_->that, $_->theOther for @objs;

The basis of idea is that

  1. re-bless the object instance into each thread which avoid the cross-thread method calling problem.
  2. You store references to shared instance variables within the hash object and dereference them to read or modify them.
  3. You embed a reference to a shared object that is used as a semaphore (via lock) into each instance and lock this in each method before accessing the instance data.

I haven't had the facilities to test this on a multi-processor system, but I think it should be safe. Any feedback you can let me have would be much appreciated.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re: Passing objects between threads...any solutions? (shared objects) by BrowserUk
in thread Passing objects between threads...any solutions ? by kimanaw

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.