Madhu Reddy wrote:

> thanx for u r response...
> i did used
> use threads::shared;
>  my %bounds : shared = ();
> 
> but i didn't use following....
> 
> {
>          lock(%bounds);
>          #-- do something with %bounds
>  }
> 
> and i got following error while running script...
> 
> perl.exe application errorr
> 
> The instruction at "0x2808335c" referenced memory at
> "0x00000004". The memory couldn't be written
> 
> this error is because of not doing lock
> on %bounds ?

i an not sure if this is caused by the corrupted data or not.

> 
> when we do the lock like
> lock(%bounds)
> does it lock all the elements in hash ?
> 

apparently it does. i ran a simple script to verify this:

#!/usr/bin/perl -w
use strict;

use threads;
use threads::shared;

my %i : shared = ();
my $t = new threads(\&run);
my $s = new threads(\&play);

$t->join;
$s->join;

print "FINISHED\n";

sub run{
        print threads->tid(),"\n";
        {
                lock(%i);
                $i{$_} = $_ for(1..5);
                while(my($key,$value) = each %i){
                        print threads->tid(),": $key $value\n";
                        threads->yield;
                }
                %i = ();
        }
}

sub play{
        print threads->tid(),"\n";
        {
                lock(%i);
                $i{$_} = $_ for(10..15);
                while(my($key,$value) = each %i){
                        print threads->tid(),": $key $value\n";
                        threads->yield;
                }
                %i = ();
        }
}

__END__

%i is shared among 2 threads. the first thread (the one that runs in run()) 
has an id of 1. the second thread (the one that runs in play()) has an id 
of 2. both threads puts a few items in the shared variable %i and then 
prints them back out. as you can see, thread 1 puts items 1 to 5 into %i 
and print them out. thread 2 puts items 10 to 15 into %i and print them 
out. i lock %i and i make each thread yield to the other one after printing 
an item. the following is the output:

1
2
1: 4 4
1: 1 1
1: 2 2
1: 5 5
1: 3 3
2: 11 11
2: 10 10
2: 13 13
2: 12 12
2: 15 15
2: 14 14
FINISHED

as you can see, thread 1 correctly prints out 1 to 5 and thread 2 correctly 
prints out 10 to 15.

now if you remove the lock(%i) statement, the following output is generated:

1
2
1: 4 4
2: 13 13
1: 11 11
2: 10 10
1: 4 4
2: 1 1
1: 12 12
2: 2 2
1: 15 15
2: 14 14
1: 5 5
2: 3 3
FINISHED

see the line that reads, "1: 11 11". that means %i is corrupted between the 
two threads without lock(%i) becasue thread 1 never puts anything beyond 10 
to the %i hash.

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to