D
David Karr
Perl 5.10.0 on Cygwin 1.5.25.
I have a loop where each iteration makes 5 independent REST calls. After each of those 5 calls, some data from the result is pushed onto an array that is declared before the loop. After the loop, I iterate through the resulting array. When this is implemented without threads, this works fine.
I decided that since each of those 5 calls are independent, it might be faster if I executed each of them in a thread, and then "join"ed each of the threads at the end of the iteration, using the "void" context, as I don't need a return value from each of those (just the side effect of pushing onto the array).
the print statements seemed to show this was working fine, and it did appear to go faster, but after the loop I checked the size of the array whose contents had been pushed from each of those threads, and it was size 0.
I also tried adding "use threads::shared;" after my "use threads;" declaration, but that had no effect.
Is there anything obviously or likely wrong with my approach?
The changes I made to implement threads were pretty simple. I just added aline like this before each of the 5 blocks:
my $xxthread = threads->create(sub {
with a matching "});" at the end of each of the 5 blocks, and then a set of5 "$xxthread->join();" calls at the end of the iteration.
There were 5 different "xx" values, as I stored each thread in a separate variable.
I have a loop where each iteration makes 5 independent REST calls. After each of those 5 calls, some data from the result is pushed onto an array that is declared before the loop. After the loop, I iterate through the resulting array. When this is implemented without threads, this works fine.
I decided that since each of those 5 calls are independent, it might be faster if I executed each of them in a thread, and then "join"ed each of the threads at the end of the iteration, using the "void" context, as I don't need a return value from each of those (just the side effect of pushing onto the array).
the print statements seemed to show this was working fine, and it did appear to go faster, but after the loop I checked the size of the array whose contents had been pushed from each of those threads, and it was size 0.
I also tried adding "use threads::shared;" after my "use threads;" declaration, but that had no effect.
Is there anything obviously or likely wrong with my approach?
The changes I made to implement threads were pretty simple. I just added aline like this before each of the 5 blocks:
my $xxthread = threads->create(sub {
with a matching "});" at the end of each of the 5 blocks, and then a set of5 "$xxthread->join();" calls at the end of the iteration.
There were 5 different "xx" values, as I stored each thread in a separate variable.