Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: returning from a thread ?

by liz (Monsignor)
on Aug 21, 2003 at 11:33 UTC ( #285432=note: print w/replies, xml ) Need Help??

in reply to returning from a thread ?

...the difference between a thread and a process...

If you're using Perl threads, you shouldn't have to worry about whether it is actually implemented using processes or real threads™. That's why they're "Perl threads".

Having said that, to my knowledge, Linux is the only system where you can actually think of threads as processes, as having seperate program id's (pid's, $$). This allows modules as Thread::Signal to function in that environment. Win32 only knows about real threads and mimics fork() using threads. Other *nixes are somewhere inbetween. Some can fork() and have real threads (Solaris seems to fall in that group). But e.g. on Mac OS X (and presumably other BSD's), threads look as seperate processes, but can not be signalled.

...never-ending sub (ie it's got a while (1) loop)m how can I stop it nevertheless?

Use a shared variable in the while. So:

while (1) {
while ($sharedvar) {
and reset the shared variable in another thread when you want to have the thread in question stop.

In that respect, you might also want to have a look at Thread::Queue::Monitored and/or Thread::Conveyor::Monitored.

Hope this helps.


I forgot one other way to do this. This will only work if you are running under Linux for now. Use Thread::Signal to activate a handler that will do a Thread::Exit. No nice cleanup that way, but definitely effective ;-)

Replies are listed 'Best First'.
Re: Re: returning from a thread ?
by Foggy Bottoms (Monk) on Aug 21, 2003 at 14:03 UTC
    I didn't manage to use the shared variable "thinggie" - as I don't have the shared package and since I didn't find it on the ActiveState repository...
    Instead I used a file in which I'm writing a byte (0 for inactive 1 for active)( I know you're thinking it's way too slow but it works fine anyhow...)

    However when the new thread starts up, it blocks the main program and I can't do anything else.
    I know someone already posted a node about this but I can't seem to find it anymore (bad luck :~/ )
    If you've any idea, let me know, and thanks again for your help...
      Strange. Which version of Perl are you using on which platform? The main program should not block!


        Here's my bit of code below :
        # now stop the current scan thread # to do this, write in a file the value 0 if (defined $thread) { open (THREADCOM, ">".LTG::dialog::THREADCOM) or die "Can't creat +e/overwrite file: $!\n"; print (THREADCOM,"0"); close THREADCOM; # stop thread } print "create thread\n"; $thread = threads->new(\&LTG::scanChange::monitor());# \&LTG::scanC +hange::monitor); $thread->detach(); # let go off the new thread
        And here's the sub's code
        sub monitor { my @wantedF; # folders to be scanned my @bannedF; # banned folders not to be scanned if ( -e LTG::dialog::CONFIGFILE) { open(FOLDERLIST,LTG::dialog::CONFIGFILE) or die "can't open file +"; my $content = <FOLDERLIST>; my $addForce = 0; while ($content) { if ($content ne "\n") { $content =~ s/\n/ /; ($content =~/^\#FORCE\#/)?($addForce = 1):(($content =~/^\# +BAN\#/)?($addForce = 0):($addForce?push(@wantedF,$content):push(@bann +edF,$content))); } $content = <FOLDERLIST>; } close (FOLDERLIST); } open (THREADCOM, ">".LTG::dialog::THREADCOM) or die "Can't create/o +verwrite file: $!\n"; print THREADCOM "1"; close THREADCOM; my $active = 1; while ($active) { if ( -e LTG::dialog::THREADCOM) { open(THREADCOM,LTG::dialog::THREADCOM) or die "can't open fil +e THREADCOM"; $active = <THREADCOM>; print "active : $active \n"; close THREADCOM; } my %changes = LTG::scanChange::startScan(\@wantedF,\@bannedF,INC +_SUBDIRS); foreach my $k (keys %changes) { print "key : $k -> content : $changes{$k}\n"; } # update database here Sumith # print "---while---\n"; } }
        I'm running perl on a Winnt 4.0 system, and here's my perl info (perl -v)
        This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail) Copyright 1987-2001, Larry Wall Binary build 635 provided by ActiveState Corp. http://www.ActiveState. +com Built 15:34:21 Feb 4 2003 Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at, the Perl Home Pa +ge.
      Not slow, just ugly.. Very, very ugly..

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://285432]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-08-09 20:48 GMT
Find Nodes?
    Voting Booth?

    No recent polls found