I am trying to use threads while assigning an array to a hash and another array to a hash of hash.
#!/usr/bin/Perl use strict; use threads; use threads::shared; my %hash :shared; my %hash1 :shared; my $a = "abcdef"; my $b = "ghighi"; my $len = length($a)-2; # Define the number of threads my $num_of_threads = 2; # use the initThreads subroutine to create an array of threads. my @threads = initThreads(); # Loop through the array: foreach(@threads){ # Tell each thread to perform our 'doOperation()' subr +outine. $_ = threads->create(\&doOperation); } # This tells the main program to keep running until all threads have f +inished. foreach(@threads){ $_->join(); } print "\nProgram Done!\nPress Enter to exit"; $a = <>; ####################### SUBROUTINES ############################ sub initThreads{ my @initThreads; for(my $i = 1;$i<=$num_of_threads;$i++){ push(@initThreads,$i); } return @initThreads; } sub doOperation{ # Get the thread id. Allows each thread to be identified. my $id = threads->tid(); my $i = 0; while($i < 2) { my $split = substr($a, $i, $len); my $split1 = substr($b, $i, $len); my $affix = substr($split, 0, $len-1); my $postfix = substr($split, 1, $len-1); my $affix1 = substr($split1, 0, $len-1); my $postfix1 = substr($split1, 1, $len-1); print "$affix\t$postfix\n"; lock(%hash); #lock(%hash1); $hash{$affix} = threads::shared::shared_clone([$affix1]); print "$hash{$affix}\n"; #$hash1{$affix}{$postfix} = threads::shared::shared_clone([$af +fix1]); This is where the problem is. How can I assign an array to a +hash of hash $i++ } print "Thread $id done!\n"; # Exit the thread threads->exit(); } Thread 1 terminated abnormally: Invalid value for shared scalar.
How can I assign an array to a hash of hash when using threads.

In reply to Multithreading How do I share hash of hash of arrays by kdarbs

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.