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.
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.# 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' => ... } );
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'?
Does the $grandchild thread inherit its stack size from its parent ($child = 2 million) or does it use the 'global' default (1 million)?# 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 = @_; ... }
In reply to New threads->create() syntax by jdhedden
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |