That's why I can't declare explicitly ahead, nor a clone of the current helps

You don't have to declare ahead, and only have to shared_clone (references to) "composite" fragments (arrays and hashes) which are being appended at each step. Here 3 workers are happily building (somewhat elaborate example, the best I came up with now) shared AoA, where, for simplicity and brevity, appended parts have rigid structure and depth is tracked, but that won't be necessary in practical implementation. Leading digit is creator worker ID, appended digits are IDs of workers which have seen this fragment, and demonstrably were able to modify it. And of course "locking" happens here because of coordinated sleeping pattern, also for simplicity. I hope I understood your requirements right, and building shared common structures in parallel will, indeed, lead to any speed gain.

use strict; use warnings; $| = 1; use threads; use threads::shared; use Data::Dumper; $Data::Dumper::Indent = 0; my $root = shared_clone [ threads-> tid ]; my $depth: shared = -1; sub worker { my $i = threads-> tid; while ( 1 ) { my $ary = $root; for ( 0 .. $depth ) { $ary-> [0] .= $i; $ary = $ary-> [1] } $ary-> [0] .= $i; $ary-> [1] = shared_clone [ $i ]; $depth ++; print '*'; sleep 3; } } for ( 1 .. 3 ) { threads-> create( \&worker )-> detach; sleep 1 } sleep 2; print "\n", Dumper( $root ), "\n"; __END__ ****** $VAR1 = ['0123123',['123123',['23123',['3123',['123',['23',[3]]]]]]];

In reply to Re: use Devel::Pointer in threads by vr
in thread use Devel::Pointer 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!
  • 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.