|new_thread ( $options = (), $code, $pass_socket, @param) -> ($thread, $socket)||
A special replacement for thread-> create, that not only creates a
thread, but also creates a socket between the parent and child threads. The
socket is important for getting an asynchronous notification when the child
thread has finished, because there is no portable way to get that signal
otherwise. That means that this socket must be closed and the thread must be
joined to avoid problems. For example:
Note that join is a blocking call, so one needs to be sure that the thread indeed is finished before joining it. By default, the child thread closes its side of the socket, thus making the parent side readable. However, the child code can also hijack the socket for its own needs, so if that functionality is needed, one must create an extra layer of communication that ensures that the child code is properly exited, so that the parent can reliably call join without blocking (see IO::Lambda::Message, that is destined exactly for this use).
$code is executed in another threads context, and is passed the communication socket ( if $pass_socket is set to 1 ). $code is also passed @param. Data returned from the code can be retrieved from join.
|threaded($code) :: () -> ( @results )||
Creates a lambda, that executes $code in a newly created thread.
The lambda finishes when the $code and the thread are finished,
and returns results returned by $code.
Note, that this lambda, if terminated between after being started and before being finished, has no chance to wait for completion of the associated thread, and so Perl will complain. To deal with that, obtain the thread object manually and wait for the thread:
|thread($lambda)||Returns the associated thread object. Valid only for lambdas created with threaded.|
|socket($lambda)||Returns the associated communication socket. Valid only for lambdas created with threaded.|
Unbalanced string table refcount Threading in Perl is fragile, so errors like the following:
Unbalanced string table refcount: (1) for "GEN1" during global destruction
are due to some obscure Perl bugs. They are triggered, in my experience, when a child thread tries to deallocate scalars that it thinks belongs to that thread. This can be sometimes avoided with explicit cleaning up of scalars that may be visible in threads. For example, calls as
undef $my_lambda; # or other scalars, whatever
inexplicably hush these errors.
Perl exited with active threads Errors like this
Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached
Scalars leaked: 1 This is a known bug, f.ex. <http://rt.perl.org/rt3//Public/Bug/Display.html?id=70974> suggests adding the @_ = (); construct at random places as a workaround. panic: del_backref during global destruction This is a known bug, <http://rt.perl.org/rt3/Ticket/Display.html?id=70748> . I observed in on win32 only. No workaround is known however. AnyEvent AnyEvent doesnt work with threads, so this module most probably wont work too when AnyEvent is selected for IO::Lambda::Loop.
Dmitry Karasik, <firstname.lastname@example.org>.
|perl v5.20.3||IO::LAMBDA::THREAD (3)||2012-03-14|