Objective: Build a thread shared hash of arrays.
The true application is a network port monitor.

The "key" is the connection properties, the array elements are byte counts and states and such.

Below is the minimal code for the errors. Subroutine createContract() is called once as a simple subroutine, and then started as a thread.

As is; the listed errors result.

If ':shared' is removed from %Contract, and '&share' is removed from the assignment within the sub, the code works fine (as expected).

?? So, what is the proper form for sharing a hash of arrays between threads
?? Simpler yet, how to read back what is stored, regardless of threads

#!/usr/bin/perl -w use threads; use threads::shared; use Carp; my %Contracts :shared; # hash holding allowed connections my $key; &createContract; my $monitor_thr = threads->create( \&createContract ); my $res1 = $monitor_thr->join(); #==================================================================== sub createContract { $key = "123456789"; # If you want to share a newly created reference unfortunately # you need to use "&share([])" and "&share({})" syntax due to # problems with Perl's prototyping. # man page $Contracts{$key} = &share([100,200,300,400]); printf (" %s %u^%u^%u^%u^\n", $key, $Contracts{$key}[0], $Contracts{$key}[1], $Contracts{$key}[2], $Contracts{$key}[3], ); } ----------------------- # [oldcode]# perl minThreadHash # printf (...) interpreted as function at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # 123456789 0^0^0^0^ # Use of uninitialized value in printf at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # Use of uninitialized value in printf at minThreadHash line 28. # 123456789 0^0^0^0^
Line # 28 is the printf(.....) line.

In reply to Shared Hash-of-Arrays between threads? by Wiggins

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.