Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: use Devel::Pointer in threads

by vr (Curate)
on Apr 05, 2021 at 13:07 UTC ( [id://11130837]=note: print w/replies, xml ) Need Help??


in reply to use Devel::Pointer in threads

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]]]]]]];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130837]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found