share semaphore across threads

PERL

    Next

  • 1. formats and write()
    Hi, I've a question involving formats. I have a text file that has lines like this: __LINE1__ __LINE2__ I use s/__LINE(\d+)__/$line{$1}/ to change each of these lines by an scalar that is stored in a hash, i.e: line{1}="CUSTOMER_NAME | PHONE | EMAIL | COUNTRY"; But, as you may have guessed, I need to format $line{1} with Perl formats. But write() writes to a filehandle, and I need to store the result into the scalar variable, How can I do it? TIA, --- Dani Pardo, XXXX@XXXXX.COM Enplater S.A
  • 2. Checking for keyboard input without halting process
    I've racked my brain for an answer to this one. Probably real simple..... I'd like to run a process reatedly until entering a character on the keyboard (other than ^C). When the character is entered the process would complete some final tasks and exit the program. do { # a process $Keyboard_Input = (check_for_input); } until ($Keyboard_Input); # some final tasks I've tried to use the standard input file <STDIN> without success. I've tried $Keyboard_Input = <STDIN>; which just halts the script until the Enter key is pressed. $Keyboard_Input = eof (STDIN); which halts until the Enter key is pressed and then returns a null character thereafter $Keyboard_Input = tell (STDIN); which returns a '0' character even if the Enter key is pressed, OK, so what am I missing here? Do I need to use something other than the Standard Input File structure?
  • 3. Help ME! --> Alas - Backreferences are Lexically scoped
    Hi, Read with Patience. Backreferences( $1, $2 ...) are lexically scoped, which means in a match like m/(<REGEX1>)(<REGEX2>) etc ..../ Suppose REGEX1 does not match, then $1 remains undefined, and if REGEX2, 3 ... do match then $2, $3 will respectively hold the matches in parentheses 2, 3 etc. My problem is say $some_var = "ababababababab"; ##here ab occurs 7 times if I say @get_match_list = ( $some_var =~ m/(ab){5,14}/g ); ## i.e get a least 5 matches and at most 14 @get_match_list will contain only one val i.e "ab", that's b'coz $1 is lexically scoped to the first( the only ) parentheses in the reg-exp. So, $1 is overwritten on each match. IN SUCH A SCENARIO, IS IT POSSIBLE TO GET ALL THE MATCHES THANKS IN ADVANCE, Sumanth Sharma
  • 4. Looking for a equivalant function in Perl for main(int argc, char *argv[ ])
    Hi, In C programming language we have a function which looks like this: main(int argc, char *argv[ ]). Is there a equivalant function in Perl. I was looking at @ARGV but i feel it does not behave the same way as the above function does. I might be wrong. Thanks in advance Shilpa

share semaphore across threads

Postby natevw » Thu, 15 Feb 2007 07:35:40 GMT

I have a list of data that I want to operate
concurrently on, as many threads as I have processors.
So I wrote a bit of code that uses a semaphore to halt
spawning of new threads when the limit are already
running.

I have two problems with the following code. First of
all, it "dead"blocks on '$available->down()', since
the thread apparently gets a copy of the semaphore
(I've tried 'my $available :shared=...' and
variations, with no change). How can I get my threads
to up() the semaphore before returning?

Secondly, after each thread returns I get a "Scalars
leaked: 1" internal Perl error on the console. Is this
a sign that I'm doing something unwelcome? How do I
get it to stop leaking scalars?


thanks,
-natevw

==========

#!/usr/bin/perl

use warnings;
use Thread qw(async);
use Thread::Semaphore;
use Data::Dumper;

sub tothread {
 print "$_[0]\n";
 sleep(1);
}

doThreads(\&tothread,[10,9,8,7,6,5,4,3,2,1]);

sub doThreads {
 my ($function,$values)=@_;
 
 my $processors=4;
 my $available=new Thread::Semaphore($processors);
 foreach my $val (@$values) {
  $available->down();
  my $t = async { $function->($val); $available->up();
print "$available is ",Dumper($available); };
  $t->detach();
 }
 for (my $i=0; $i<$processors; $i++) {
$available->down(); }
}

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ratis! 
Regtrate ya -  http://www.**--****.com/ 

Re: share semaphore across threads

Postby natevw.yahoo » Thu, 15 Feb 2007 10:11:12 GMT

On 2007-02-13 14:35:40 -0800,  XXXX@XXXXX.COM  (Nathan Vander Wilt) said:


Found out my Camel book didn't have the latest thread info, so switched 
to 'use threads;' and that solved this first problem.




This seems to be related to the '$t->detach()', which I'm using because 
I don't care about the return value, am having trouble with growing 
memory usage, and I thought telling perl would help it out. (This 
happens on both 5.8.0 and 5.8.6.)

-nvw




Re: share semaphore across threads

Postby zentara » Fri, 16 Feb 2007 02:00:29 GMT





From my experience, it is one of the problems with the way
Perl handles threads, it will pick up memory for each
thread spawned, and won't return it to the Operating System.

The best thing to do, is to reuse your threads. Don't detach them,
instead just reload them with new data to process, thru shared
variables.  So normally you would start a x number of threads,
and keep using them over and over.

Perl introduces some problems with threads, that you won't see
from c.  


-- 
I'm not really a human, but I play one on earth.
 http://www.**--****.com/ 

Similar Threads:

1.threads & threads::shared & Threads::Semaphore

Hi, All,

Hope everyone's new year is starting out very well.

I'm having to adjust a perl program I wrote to manipulate some  
genetics data.  Originally, the program had no memory problems but now  
that I've added a couple more hashes, I'm having memory issues.  It  
now runs out of memory when it's about half way through processing the  
data.  Unfortunately, the data is very interconnected and the  
statistics I need to execute involves data from several of the  
hashes.  I'm thinking of using threads & threads::shared in order to  
be able to process, store and access the data among several of 8  
processors on a Sun Spark system.  Of course, I'll need to install the  
threads and threads::shared modules and possible even re-compile perl  
on this machine but before I go and do all this fun stuff, I wanted to  
ask your opinion about whether or not I'm going down the right rabbit  
hole or if I'm just digging myself a shallow grave.  Would this be the  
way you might do it?  I've also heard of Semaphores.  Might this be a  
better way to go about spreading the data in hash form among several  
processors on one machine and still be able to access the data in each  
hash from the main program?

Thanks in advance for the excellent input I always get from this  
group!  :-)

Aimee Cardenas

2.sharing semaphore with/between threads

I have a list of data that I want to operate concurrently on, as many 
threads as I have processors. So I wrote a bit of code that uses a 
semaphore to halt spawning of new threads when the limit are already 
running.

I have two problems with the following code. First of all, it 
"dead"blocks on '$available->down()', since the thread apparently gets 
a copy of the semaphore (I've tried 'my $available :shared=...' and 
variations, with no change). How can I get my threads to up() the 
semaphore before returning?

Secondly, after each thread returns I get a "Scalars leaked: 1" 
internal Perl error on the console. Is this a sign that I'm doing 
something unwelcome? How do I get it to stop leaking scalars?


thanks,
-natevw

==========

#!/usr/bin/perl

use warnings;
use Thread qw(async);
use Thread::Semaphore;
use Data::Dumper;

sub tothread {
 print "$_[0]\n";
 sleep(1);
}

doThreads(\&tothread,[10,9,8,7,6,5,4,3,2,1]);

sub doThreads {
 my ($function,$values)=@_;

 my $processors=4;
 my $available=new Thread::Semaphore($processors);
 foreach my $val (@$values) {
  $available->down();
  my $t = async { $function->($val); $available->up(); print 
"$available is ",Dumper($available); };
  $t->detach();
 }
 for (my $i=0; $i<$processors; $i++) { $available->down(); }
}

3.how to share socket handle across threads?

Hello,

I've been rtfm'ing for days but can't a solution to this problem:

I have a thread that receives multiple client request thanks to an
endless loop through the accept() method. The new socket object
returned by accept() must be communicated to another master thread. I
am using the IO::Socket module.

When trying to use threads::shared or Thread::Queue, I keep getting
this failure:
Invalid value for shared scalar at ./X.pl line N.

So is there a way to share a socket/file handle accross threads?

Thank you

Sebastien

4.shared variables in threaded tk not being shared

Hi,

Wonder can someone help. I've been trying a few things with perl/tk and
threads with some success but cant understand a particular problem.
I've read various posts on the subject and have altered my script
accordingly but still no go! Basically the script creates some shared
variables, a sleeping thread first, then the tk window and widgets.
The thread essentially starts to update a shared variable every second
once the start button is pressed. I can see this by printing to STDOUT.
A label in the GUI points to this shared variable and is instructed to
update it.
However if i put in some debug print statements i can see that in
effect this variable is not shared as it retains the initial value only
hence the label may be updated but with the same value.
Here is the code:

use Tk;
use threads;
use threads::shared;
use strict;

my $go : shared = 0;
my $myValue : shared = 0;

my $anotherThread = threads->new(\&ticker);

sub ticker {
	while (1) {
		if ($go == 0) {sleep 1;}

		else {
			$myValue++;
			print "$myValue\n";
			sleep 1;
		}
	}
}

my $mw = MainWindow->new();
$mw->title('UDP Sniffer');

my $f1 = $mw->Frame(-borderwidth => 2, -relief => 'groove')->pack(-side
=> 'top', -fill => 'x');
my $label2 = $f1->Label(-textvariable => \$myValue, -width =>
15)->pack(-side => 'left');
$f1->Button(-text => 'Start', -command => \&start)->pack(-side =>
'left');
$f1->Button(-text => 'Stop', -command => \&stop)->pack(-side =>
'left');
$f1->Button(-text => 'Reset', -command => \&resetme)->pack(-side =>
'left');

MainLoop();

sub start {
	$go = 1;
	print "In the main window - $myValue\n";
 	##$mw->repeat(1, sub {$mw->update;});
}

sub stop {
	$go = 0;
}

sub resetme {
	$myValue = 0;
}

Threads are quickly becoming annoying to use as it seems they present
more problems than solutions with tk so could someone tell me what
other options i have for something similar to the above?

Thanks in advance

5.Threads, semaphores, and global variables

I have a program that uses semaphores to control the behavior of
threads accessing shared data.  I can see that a global variable
(statistics counter) is changing in a function, but it isn't changed
outside the function.

$stat_ctr = 0;
$sem  = new Thread::Semaphore;

sub ThisThreadsFunction {
   $sem->down();
   $stat_ctr++;
   # prints "1"
   print $stat_ctr . "\n";
   $sem->up();
}

$p  = threads->create("ThisThreadsFunction", "argument");

# prints "0"
print $stat_ctr . "\n";

Any ideas on why this code is behaving as if there is a local
$stat_ctr variable when there is clearly a global $stat_ctr?

Thanks.
-trek

6. Thread::Semaphore consumes memory

7. how to share variable across multi-processes

8. mod_perl: sharing data across httpd childs ?



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 24 guest