in reply to Re: OO theory question
in thread OO theory question

Edit2, wooohah! iet ies alive! now if only i can find a way to make it stop dying because the main script ends i'm all the way there :/

Edit, problem solved by using threads->create(\&_run); instead

well, i went for your suggestion concerning threads, and all seems to be fine, except for one thing, the thread doesn't return control to the calling subroutine :(

I've spent quite a bit of time reading tutorials and the like, and they all tell me that when using the threads module the calling script should continue normally after spawning off a thread...here's the bit of code that i use:
sub run { my $self = shift; $self->{thread} = threads->create($self->_run); print "does this ever show up?\n"; }
The print statement never gets executed, as the thread takes control of the entire script. Any ideas?

Replies are listed 'Best First'.
Re^3: OO theory question
by stvn (Monsignor) on Jun 10, 2004 at 13:55 UTC

    Sorry, threads aren't my thing, so I am not the best person to answer your question. But I think showing us your _run method might be helpful in helping solve this problem. I can give you a basic example of how one would do this with fork:

    sub run { my ($self) = @_; my $PID = fork(); # fork returns undef if it fails (defined $PID) || die "Could not fork successfully"; # fork returns the Process ID of the new process it # has created to the parent process, and 0 to the # child process, so if we have a "true" value in $PID # then we are in the parent if ($PID) { # so we let the parent store the PID # of the process which is "_run"-ning $self->{PID} = $PID } else { # otherwise we are in the child, so we # should call _run $self->_run(); # we sure to call exit here so our child # process cleans up nicely exit(); } } # you can call stop from the parent # process to kill the child, you can adapt # this to send any number of signals to # the child depending upon your needs sub stop { my ($self) = @_; kill "INT", $self->{PID}; }
    Here are a couple of links to relevant documentation:

    -stvn