There are a couple of pretty significant problems with your code.

... print "create thread\n"; my $run : shared = 1; $thread = threads->new( \&LTG::scanChange::monitor()); # \&LTG::scanChange::monitor); print "Waiting 5 seconds while the thread runs\n"; sleep 5; print "Shutting down\n"; $run = 0; $thread->join; }

First, this line

$thread = threads->new(\&LTG::scanChange::monitor());

By adding the parens to the end of the sub name, you are completely changing the meaning of this line. vis:

sub test{ print 'Hello world' } print \&test; CODE(0x1bc2cdc) print \&test() Hello world SCALAR(0x1bc2d18)

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.

thread failed to start: Not a CODE reference at ...

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

# Do some stuff $thread->new( ...); # start a thread sleep ...; # Sleep a while $thread->join; # Wait for the thread to finish. # do some other stuff.

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.


In reply to Re: Returning from a thread - here's the code... by BrowserUk
in thread returning from a thread ? by Foggy Bottoms

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.