In Use more threads., BrowserUk wrote:
In the long term, a better solution would be for threads::create() to accept an extra (named?) parameter that allowed the Perl programmer to specify the stack reservation on a per thread basis.
I'm working on breaking out threads into a separate CPAN module. (Just as was done with threads::shared.) Therefore, now would be a good time to consider making this sort of a change.

I'd like some ideas on what would be a reasonable syntax for named arguments.

# Possibly named argument syntax: threads->create('stack' => $size, 'code' => 'function', # or sub{} or \&func 'args' => ...); # Or using a hash ref as suggested in PBP: threads->create( { 'stack' => $size, 'code' => 'function', # or sub{} or \&func 'args' => ... } );
In the above, if the value for 'args' is an array ref, its contents would be ('unrolled' and) passed to the function as a list. Any other scalar would be passed as is.

Of course, the old syntax of threads->create(FUNC, ARGS...) would be available for backward compatibility.

Update: The more I think about it, the more questions come to me. renodino proposed using a couple of class functions to get/set a 'global' default for the stack size.

With these included in the equation, creating a thread from the 'main' thread without using the 'stack' parameter would use whatever was previously set for the default. However, what about a child thread created from a thread that used 'stack'?

# Set the 'default' thread stack size threads->set_stack_size(1_000_000); # Create a thread with a bigger stack my $child = threads->create('stack' => 2_000_000, 'code' => 'child_thread', 'args' => ...); sub child_thread { my @args = @_; ... # What's my stack size? my $grandchild = threads->create('code' => 'grandchild_thread', 'args' => ...); ... } sub grandchild_thread { my @args = @_; ... }
Does the $grandchild thread inherit its stack size from its parent ($child = 2 million) or does it use the 'global' default (1 million)?

Remember: There's always one more bug.

In reply to New threads->create() syntax by jdhedden

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.