For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2006 > threads









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author threads
Octavian Rasnita

2006-03-21, 7:55 am

Hi,

I have tried the following script:
use threads;
my $threads = async {foreach(1 .. 70) {download($_)}};

$threads->join();
$threads->detach();

sub download {...}

This program takes the same amount of time to run like when not using
threads, but a simple for() loop.

How can I make it to run all the threads in the same time?

Thank you.

Teddy

Stephen Kratzer

2006-03-21, 9:58 pm

I'm not too familiar with threads, but I'll give it a go. You're still
executing the download() sub seventy times one after another rather than
concurrently. The async function will created one new thread for the block
following it. You might try a for loop which creates a new thread at each
iteration.

Maybe something like:

my @threads;

for (my $i = 1; $i <= 70; $i++) {
$threads[$i] = threads->create(\&download, $i);
$threads[$i]->join();
}

On Tuesday 21 March 2006 06:39, Octavian Rasnita wrote:
> Hi,
>
> I have tried the following script:
> use threads;
> my $threads = async {foreach(1 .. 70) {download($_)}};
>
> $threads->join();
> $threads->detach();
>
> sub download {...}
>
> This program takes the same amount of time to run like when not using
> threads, but a simple for() loop.
>
> How can I make it to run all the threads in the same time?
>
> Thank you.
>
> Teddy

Octavian Rasnita

2006-03-21, 9:58 pm

From: "Stephen Kratzer" <kratzers@pa.net>

> I'm not too familiar with threads, but I'll give it a go. You're still
> executing the download() sub seventy times one after another rather than
> concurrently. The async function will created one new thread for the block
> following it. You might try a for loop which creates a new thread at each
> iteration.
>
> Maybe something like:
>
> my @threads;
>
> for (my $i = 1; $i <= 70; $i++) {
> $threads[$i] = threads->create(\&download, $i);
> $threads[$i]->join();
> }
>


I have tried this method, but it executes much slower than a simple for()
loop, with no threads at all.

I have tried it using perl, v5.8.7 built for MSWin32-x86-multi-thread.

Thank you.

Teddy

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com