Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
... 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.

  • Every method call involves at least 2 context switches.

    Which make for very slow, computationally expensive objects.

  • Methods can be called concurrently from different threads, which means you need to add locking and synchronisation to every method.

    That makes the object slower still.

    Makes them complex to write, test and maintain.

  • Makes your threads run in 'lock-step'.
    • Thread 1 does some stuff, the calls a method on a shared object. It now has to block to wait for the method to return.
    • A context switch (eventually) occurs,
    • Thread 2, the 'object thread', which has been spinning its heals (polling) waiting for something to do, executes the (usually trivial) method code and returns the value; and goes back to polling.
    • A context switch occurs.
    • Thread 1 gets the return value from the method.

    It completely defeats the primary benefit of using threading; that of allowing the threads to do work simultaneously.

    Where is the benfit of using an 'object thread', if every time the caller thread calls a method, it has to block until the object thread gets swapped in, does it thing and returns the result?

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found