Advertisement
Advertisement
| 02.14.2008 at 09:42AM PST, ID: 23163713 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: |
#!/usr/bin/perl
use threads;
use threads::shared;
my %shared_cache;
share(%shared_cache);
for (my $i; $i < 10; $i++)
{
my $result = threads->create(\&thread_function, $i);
}
sub thread_function($)
{
my $var = shift;
if ($shared_cache{$var})
{
print "found cached value\n";
}
else
{
print "no cached value, calculating and caching\n";
$shared_cache{$var} = calculate_result($var);
}
return $shared_cache{$var};
}
|