Try this. I've used a randomly variable 'work pause', as if this is constant, the threads will obviously finish in the same order as they started.

Update: Applied locks per ikegami's post below.

#! perl -slw use strict; use threads; use threads::shared; my $content : shared = ''; sub concatenate_parallel { my $letter = shift; sleep 1 + rand 2; ## Do stuff that takes time { lock $content; $content .= $letter; } } print scalar localtime; my @threads = map{ threads->create( \&concatenate_parallel, $_ ) } 'a' + .. 'c'; $_->join for @threads; print $content; print scalar localtime; __END__ c:\test>579015 Wed Oct 18 18:01:20 2006 cab Wed Oct 18 18:01:22 2006 c:\test>579015 Wed Oct 18 18:01:24 2006 bca Wed Oct 18 18:01:26 2006 c:\test>579015 Wed Oct 18 18:01:27 2006 abc Wed Oct 18 18:01:29 2006 c:\test>579015 Wed Oct 18 18:01:30 2006 abc Wed Oct 18 18:01:32 2006

If you don't like the shared buffer being passed to the threads through closure--it smacks of globals--then you could use this version that passes a reference to the shared buffer to the threads as an argument and dereferences it when appending:

#! perl -slw use strict; use threads; use threads::shared; sub concatenate_parallel { my( $contentRef, $letter ) = @_; sleep 1 + rand 2; ## Do stuff that takes time { lock $contentRef; $$contentRef .= $letter for 1 .. 1e4; } } my $content : shared = ''; my $contentRef = \$content; print scalar localtime; my @threads = map{ threads->create( \&concatenate_parallel, $contentRef, $_ ); } 'a' .. 'z'; $_->join for @threads; print $content; print scalar localtime; __END__ c:\test>579015 Thu Oct 19 00:33:11 2006 cbefghlmqsvwadijknoprtuxyz Thu Oct 19 00:33:13 2006

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".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: using parallel processing to concatenate a string, where order of concatenation doesn't matter by BrowserUk
in thread using parallel processing to concatenate a string, where order of concatenation doesn't matter by tphyahoo

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.