Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
For the sub{} share stuff, I am still not sure it's necessity.

Necessity or not -- though it never is -- it is impossible.

If I give anything try to alter the cloned structure, it give the Thread 2 terminated abnormally: Invalid value for shared scalar error.

You are trying to add a non-shared hash into a shared array: push @s, { $cmd => time }; ; and that it illegal.

You need to share() or share_clone that data you are adding:

push @s, &share( { $cmd => time } }; ## The & before share() is import +ant here; and one of the few times you should ever use it. ## or push @s, &share_clone( { $cmd => time } }; ## or my %hash : shared = ( $cmd => time() ); push @s, \%hash;
This kind of assessment ( shareable R/W stuct ) would be more prior for my needs.

Sorry, but I'm having trouble understanding the meaning of that sentence?

And for my ultimate purpose, I wish to share a struct which carrying different on-the-fly created objects, and they can cross access each other.... However, this leads me to think, is if there's any transparent way for a complex stuct become shareable

Covering the last sentence first: As demonstrated; it is perfectly possible to shared complex data structures; and to modify them on the fly. But you do have to learn and follow the rules:

  1. Everything except scalars stored as values in shared arrays and hashes, -- ie. any references to nested structures -- must themselves be shared:
    my @array : shared; $array[ 0 ] = 'fred'; ## ok $array[ 1 ] = 12345; ## ok $array[ 2 ] = [ 1,2,3 ] ## NOT OK $array[ 2 ] = &share( [ 1,2,3 ] ); ## OK $array[ 3 ] = { 'a'..'z' }; ## NOT OK my %hash : shared = ( 'a' .. 'z' ); $array[ 3 ] = \%hash; ## OK ...
  2. When making modifications to shared structures; you must lock the parent structure to avoid collisions:
    lock @s; push @s, &share( { $cmd => time() } ); ... lock %{ $s[ 9 ] }; $s[ 9 ]{ $cmd } = $newtime;
Of cause I can change the style to share an array to indicate a "queue" and then every objects go inside the queue to look for action.

Shared data structures often seem like a good solution. The problem with them is how does one thread know when one of the other threads has modified something that it needs to look at?

The typical naive (Java-esque) approach to this problem is some elaborate setup using condition variables and locking and signaling and waiting; ie. synchronisation.

The problem with that is that it difficult to get right; prone to deadlocks, livelocks and priority inversions; and even when it is programmed correctly, it is usually ponderous because each communication of state -- condition signaling -- requires at least 3 context switches to occur. And with sod's law and competing threads and processes vying for processor time, often many more.

NB: these problem with synchronisation are not particular to Perl's shared state implementation; but also every other shared state mechanism; Indeed, they are far less prevalent with Perl due to its pretty unique explicitly-shared-only mechanisms; but still prevalent enough to avoid unless there is no alternative. (I've yet to see an application where there wasn't a better (usually queued) solution!)

Conversely, communications via queues is easy to reason about and get right first time; is not subject to any of the shared-state nasties above; and is efficient and effective because the right thread gets woken automatically when there is something for it to do; and lies dormant consuming no processor time when then isn't.

Finally, the problem with your application description is that you've decided that you should used shared structures and have defined it in those terms. If you would describe what you actually need to do; rather than how you think you should do it; it would be possible to suggest more effective alternatives; perhaps even point to existing similar examples or generate a small demonstration.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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.

In reply to Re^3: How to share complex data structure in threads ? by BrowserUk
in thread How to share complex data structure in threads ? by exilepanda

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (9)
    As of 2014-09-21 18:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (174 votes), past polls