Keep It Simple, Stupid | |
PerlMonks |
Re: About self-invoked threads into classby BrowserUk (Patriarch) |
on Jun 01, 2012 at 14:10 UTC ( [id://973773]=note: 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:
doesn't work, is because of what you are doing in this line:
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:
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:
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:
Which you've probably tried, only to receive the fatal error message:
The way to avoid that is to use the shared_clone() function exported by threads::shared, to shared the thread object handle, thus:
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.
In Section
Seekers of Perl Wisdom
|
|