Re: please help with ithreads

[email protected] (Khai Doan)
Newsgroups perl.ithreads
Message-ID <[email protected]>
I want to use ithread mainly for its performance.  Using multi-processes 
is really not an option for me.

The part that have to do with threads:

my @workHorse = ();
my $numThreads = 30;
my $eachThread = int(scalar(@{$urls}) / $numThreads);
my $workLoad = [];
my $i = 0;
while (scalar(@{$urls})) {
     my $url = shift @{$urls};
     push @{$workLoad},$url;
     if (scalar(@{$workLoad}) == $eachThread) {
         $workHorse[$i] = threads->new(\&worker,$workLoad);
         $workLoad = [];
         $i++;
     }
}
$workHorse[$i] = threads->new(\&worker,$workLoad);
foreach my $thr (@workHorse) {
     $thr->join();
}

Basically, the main thread loops over an array of urls, divide up the 
work among the worker threads by pushing the url onto an anonymous array 
referenced by $workLoad, and when this anonymous array contain certain 
number of urls, it starts a new worker thread and store the thread 
object in the array @workHorse.  When outside the while loop, it start 
another worker thread, then it goes inside the foreach loop to wait for 
the worker threads to finish.  Is there anything wrong with this logic? 
  Should I look elsewhere in my code?  I thought that using ithread, 
unless I explicitly share a variable, everything should be ok (global 
variable such as $_ is automatically safeguarded somehow).  Is this not 
the case? What if one of the used modules has a global variale $x that 
is not declared with my?  Would that make it thread unsafe?  In general, 
what would make a piece of code/module thread unsafe if using ithread?

Thanks
Khai


Martín Ferrari wrote:
> Hi Khai!
> 
> Your code is not very easy to follow, and is really non-self-evident
> in some places. Nevertheless, I can say that I don't see any reason to
> use ithreads, with all the problems associated, when you can just fork
> each worker. I don't see any interaction after the creation of the
> thread, so it would be the same functionality, and a lot easier to
> debug.
> 
> On 3/1/06, Khai Doan <[email protected]> wrote:
> 
>>Can someone see anything wrong with my code?  Sometimes it failed short
>>when the list of urls to check is long.  Is ithread stable ?  Here is
>>the output of perl -V :
>>
> 
> 
>>Is there any known issues with just including a non-thread-safe module
>>(but not using it)?  Is the regex engine thread safe now?
>>
>>Thanks
>>Khai
>>
> 
> 
> 
> --
> Martín Ferrari
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.