NAME
WiringPi::API::WorkerThread - Handle for an ithread-based background worker
SYNOPSIS
use threads; # required for mechanism => 'thread'
use WiringPi::API qw(setup worker);
setup();
my $w = worker(sub {
# ... body sharing state with main ...
select(undef, undef, undef, 0.1);
}, { mechanism => 'thread' });
$w->stop; # sets the stop flag and joins the thread
DESCRIPTION
An object of this class is returned by "worker(\&body, \%opts)" in WiringPi::API under mechanism => 'thread', which runs the body in an ithread for shared-memory ergonomics on a threaded Perl.
You never construct one directly - worker() spawns the thread and hands you the handle.
It is a subclass of WiringPi::API::Worker, but the thread lifecycle is nothing like a fork's (no pid, no signal/waitpid), so this class overrides pid/running/stop. There are no pipe channels under thread mode (the results and shared options are rejected at construction), so the channel accessors read/fh/value are also overridden to croak with a guiding message rather than silently return undef - share a variable and serialise access with "pi_lock($key)" in WiringPi::API / "pi_unlock($key)" in WiringPi::API instead.
METHODS
pid
Threads have no process id, so this reports the thread id (tid) instead, keeping the handle interface uniform with the fork-based handles.
running
True while the thread is still alive; false once it has stopped. A once => 1 thread exits on its own, and running joins such a self-finished thread so it reflects reality.
stop
Set the cooperative stop flag (honoured at the next pass of the body) and join the thread. Idempotent. Returns 1.
read / fh / value
Croak under thread mode. A thread worker has no pipe channels - the results and shared options are rejected when it is constructed - so there is nothing for these accessors to return. They croak with a message pointing you at shared memory plus "pi_lock($key)" in WiringPi::API / "pi_unlock($key)" in WiringPi::API, matching the way WiringPi::API::BackgroundInterrupts croaks for its absent results channel. (On a fork worker these instead return undef when the channel was not requested.)
SEE ALSO
WiringPi::API, WiringPi::API::Worker.
AUTHOR
Steve Bertrand, <steveb@cpan.org>
COPYRIGHT AND LICENSE
Copyright (C) 2017-2026 by Steve Bertrand
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.18.2 or, at your option, any later version of Perl 5 you may have available.