CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
multithreading package

 



andrijko
New User

May 7, 2010, 9:18 AM

Post #1 of 3 (890 views)
multithreading package Can't Post

Hello, i'm writing class for multithreaded program and i stuck with some memory issue. I want both threads to ended when the MAIN function call stop() .
How shuld I share $this ?
Could someone help me with this?

Package
[code]
package Edgar;
use threads;
use threads::shared;
use warnings;
use strict;

sub new
{
my %this =( _id => $_[1],_is_running => $_[2], _thr =>0);
bless(\%this,"Edgar");
}

sub get_id
{
my ($this) = @_;
return( $this->{_id});
}

sub run
{
my ($this) = @_;
if($this->{_is_running} != 1)
{
$this->{_is_running} = 1;
$this->{_thr} = threads->create(\&do_it,$this);
}
}

sub thread
{
my ($this) = @_;
return( $this->{_thr});
}

sub stop
{
my ($this) = @_;
if(!$this->{_is_running})
{
$this->{_is_running} = 0;
}
}

sub do_it
{
my ($this) = @_;
while($this->{_is_running})
{
print "\ndo_it $this->{_id}";
sleep(1);
}
print "\nThread ending ..";
return 1;
}
1;
[/code]

and edgar.pl

[code]
#!/usr/bin/perl

use Edgar;

my $a =Edgar->new(12,0);
my $b =Edgar->new(5,0);

print $a->get_id()."\n";
print $b->get_id();


$a->run();
sleep(3);
$a->stop();

$b->run();
sleep(3);
$b->stop();
[/code]
thanks !


7stud
Enthusiast

May 9, 2010, 9:20 AM

Post #2 of 3 (819 views)
Re: [andrijko] multithreading package [In reply to] Can't Post

1) Search google for 'code tags'. Read the first 10 results and come back and post what you have learned.

2) Never use the syntax: $_[1]. Unpack the arguments into variables on the first line of the sub, then do whatever needs to be done after that.

Once you get those things sorted out, I'll take a look at your code.


(This post was edited by 7stud on May 9, 2010, 9:25 AM)


andrijko
New User

May 10, 2010, 3:59 AM

Post #3 of 3 (795 views)
Re: [andrijko] multithreading package [In reply to] Can't Post

I have already solved that problem. I need to share all keys of "this" list.
Look into attachments for the working examples ..
Attachments: Edgar.pm (1.33 KB)
  edgar.pl (0.35 KB)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives