Reputation: 516
Looks like times of old good alarm() call are over, so how to interrupt blocking read()'s and write()'s made from perl thread (using that brand new 'threads' module) assuming the code making those blocking calls cannot be changed? Actual problem is stucking communication with Modbus device so I've created a simple testcase not to dip you into RS-485 hell:
use threads;
use IO::Handle;
threads->create(sub {
$io = IO::Handle->new_from_fd(fileno(STDIN), 'r') or die;
$br = read $io, $buf, 100;
warn "read: $br";
});
while(1) {threads->yield()};
Here, warn() is never executed unless you hit Ctrl-D on keyboard. Is there any simple solution to timeout that read() call?
Upvotes: 3
Views: 458
Reputation: 8532
Rather than threading (which is ill-supported in Perl at best, and mostly frowned upon), why not try one of the event modules instead? That's the standard solution to concurrency in Perl.
Upvotes: 1