kdarbs has asked for the wisdom of the Perl Monks concerning the following question:
How can I assign an array to a hash of hash when using threads.#!/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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multithreading How do I share hash of hash of arrays
by BrowserUk (Patriarch) on Jan 22, 2015 at 21:48 UTC | |
|
Re: Multithreading How do I share hash of hash of arrays
by Jenda (Abbot) on Jan 24, 2015 at 01:17 UTC | |
by BrowserUk (Patriarch) on Jan 24, 2015 at 01:41 UTC | |
by Jenda (Abbot) on Jan 24, 2015 at 11:45 UTC | |
|
Re: Multithreading How do I share hash of hash of arrays
by BrowserUk (Patriarch) on Jan 24, 2015 at 13:59 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |