
andrijko
New User
May 7, 2010, 9:18 AM
Post #1 of 3
(734 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 !
|