... function stop_thread cannot execute functions $inter->{THR}->kill('TERM') and $inter->{THR}->join() and hence, the script cannot wait to thread termination. What is wrong?

Technically speaking, the reason that this line:

$inter->{THR}->kill('SIGTERM');

doesn't work, is because of what you are doing in this line:

$inter->{THR} = ref threads->create(sub{$thread->(\$inter->{INT1}) +});

That is, instead of storing the thread handle into $inter->{THR}, you are storing the return value of ref when applied to that thread handle. And that means you are storing:

$t = async{ sleep 1000 };; print ref $t;; threads

Ie. You are storing the text string 'threads'.

Which means that when you come to try and call the instance method kill(), instead of passing an instance handle you are calling it as a class method, which (with warnings enabled), should have told you:

Usage: $thr->kill('SIG...') at HardThread.pm line 23.

It doesn't work because it doesn't know which thread to kill.

To avoid that problem, you would need to store the thread handle directly thus:

$inter->{THR} = threads->create(sub{$thread->(\$inter->{INT1})});

Which you've probably tried, only to receive the fatal error message:

Invalid value for shared scalar at HardThread.pm line 29.

The way to avoid that is to use the shared_clone() function exported by threads::shared, to shared the thread object handle, thus:

$inter->{THR} = shared_clone( threads->create(sub{$thread->(\$inte +r->{INT1})}) );

Make that change, and your code will "work".


That deals with the technical issues with your code; however, it doesn't deal with what (IMO) is a more important issue.

That issue is that the design of your module using shared objects and (pseudo-)signals, in conjunction with threads is a fundamentally flawed concept.

The explanation of why is difficult.

It is quite likely that this explanation will not convince you -- if you have used Java, the above scenario will likely seem like normal operating procedure -- but if what I've said makes sense to you, and you would like a better approach to solving your problem, then you'll need to describe that actual problem, instead of how you are currently trying to tackle it.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


In reply to Re: About self-invoked threads into class by BrowserUk
in thread About self-invoked threads into class by Omniperl

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.