How to interrupt an I/O call in a perl thread?

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?

Answers


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.


Need Your Help

pass multiple values present in ListBoxFor to controller, while postback

asp.net-mvc

I am using asp.net mvc 2.0 in my application and the application requires to get multiple values from a ListBox, from view to controller.