in reply to About self-invoked threads into class
... 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.
Which make for very slow, computationally expensive objects.
That makes the object slower still.
Makes them complex to write, test and maintain.
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: About self-invoked threads into class
by locked_user sundialsvc4 (Abbot) on Jun 01, 2012 at 19:06 UTC | |
by BrowserUk (Patriarch) on Jun 01, 2012 at 19:49 UTC |