in reply to Re: Re: Re: Re: Re: Re: returning from a thread ?
in thread returning from a thread ?
This should display (possibly in a different order:use threads; use threads::shared; my $run : shared = 1; my $thread = threads->new( sub { while ($run) { print "Hi!\n"; sleep 1; } } ); print "Waiting 5 seconds while the thread runs\n"; sleep 5; print "Shutting down\n"; $run = 0; $thread->join;
Hi! Waiting 5 seconds while the thread runs Hi! Hi! Hi! Hi! Shutting downIf it doesn't, there is a problem with threads in your environment.
Liz
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Returning from a thread - here's the code...
by Foggy Bottoms (Monk) on Aug 22, 2003 at 14:45 UTC | |
Please note that the function monitor freezes as long as there's no change on the hard drive... Should there be one, it notifies the user and loops in the while again... | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on Aug 22, 2003 at 18:54 UTC | |
There are a couple of pretty significant problems with your code.
First, this line
By adding the parens to the end of the sub name, you are completely changing the meaning of this line. vis:
In the first case, without the parens, the \&subname returns a code reference to (the address of) subname. This is required so that threads->new() knows what code to run in the new thread. In the second case, with the parens, the \&subname(), invokes the sub, in the context of the current thread--hence you see the "Hello World" printed. Then a reference is taken to the return value of the sub--which is the '1' from the print statement in this case--hence a scalar reference gets output. Passing a scalar reference to threads->new( \1 ); ought to be giving you an error message, regardless of whether your using warnings or strict.
Are you not seeing this? I note that you have the correct format of the call in a comment on the end of that same line? The second, and perhaps more fundemental error is way you are using the thread overall. Essentially, what you are doing is
What this means is that there is completely no reason for using a thread at all here. You are blocking your main thread whilst the second thread runs to completion. You would achieve exactly the same result by simply calling the sub at this point and avoiding all the sharing stuff. I'd try and offer further suggestions on how to make your code do what you want, but to be frank, it's not at all clear to me what it is that your are trying to achieve. The code you have posted is such a small window in the over all scheme of your module that it makes it difficult to see what is going on. Perhaps you could post a (brief) pseudo-code or textual description of the overall program, and explain what you are hoping to achieve by using threads. Then we might be able to suggest a workable strategy. Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller If I understand your problem, I can solve it! Of course, the same can be said for you. | [reply] [d/l] [select] |
by Foggy Bottoms (Monk) on Aug 26, 2003 at 14:47 UTC | |
Thanks for your explanation - this sort of details (the parens) can really kill you and I finished last week with the flu 0 just in time for a lovely 3-day week-end. Oh well... As for the relevancy of the codem the sleep(5) was for the sake of testing and isn't in the final code. As a matter of fact I've dropped the thread use here to implement it at a higher level when integrating perl into VC++. For the sake of this node, my purpose was to have an ongoing (neverending) thread that'd monitor the hard drive and detect whether changes occured - but this bit of code doing the monitoring blockc the rest of the code. It was then necessary to have it in a separate thread so that I could still use the GUI and do other stuff... Thanks again for your insight and surely see you soon round another node. | [reply] |