Note that there are some explanatory texts on larger screens.

plurals
  1. POIs the select() wrapper in IO::Select thread-safe? How to work around?
    primarykey
    data
    text
    <p>Let's say I have a thread:</p> <pre><code>sub new { my $class = shift; my $self = ref $class || $class; bless { 'read_set' =&gt; IO::Select-&gt;new, 'write_set' =&gt; IO::Select-&gt;new, 'error_set' =&gt; IO::Select-&gt;new }, $self; } sub start { my $self = shift; $self-&gt;{'thread'} = threads-&gt;create(\&amp;worker_thr, $self); $self-&gt;{'thread'}-&gt;detach; } sub worker_thr { my $self = shift; while(1) { my($read_active, $write_active, $error_active) = IO::Select-&gt;select( $self-&gt;{'read_set'}, $self-&gt;{'write_set'}, $self-&gt;{'error_set'}, undef ); } } </code></pre> <p>Since I have provided no timeout to <code>select()</code> and it blocks indefinitely until activity is detected on one of the descriptor (well, "handle") sets, what happens if another thread modifies the contents of the sets (e.g. adds a new socket for polling)? </p> <p>Is it implemented in a thread-safe manner, relative to the Perl POSIX threads implementation?</p> <p>If not, I suppose I can stick the blocking <code>select()</code> call in a block with its own scope and lock the sets or, more cleanly, the entire package data. What is the best way to wake <code>select()</code> up to return, from another thread so that the contents of the sets can be manipulated? Thread-specific signal? Semaphore? Suggestions welcome.</p> <p>Thanks muchly!</p> <p><strong>Edit:</strong> Well, thread-specific "signal" is out. As explained here ( <a href="http://perldoc.perl.org/threads.html" rel="nofollow noreferrer">http://perldoc.perl.org/threads.html</a> ): "Correspondingly, sending a signal to a thread does not disrupt the operation the thread is currently working on: The signal will be acted upon after the current operation has completed. For instance, if the thread is stuck on an I/O call, sending it a signal will not cause the I/O call to be interrupted such that the signal is acted up immediately."</p> <p>That still leaves me wondering how I should handle the issue of waking <code>select()</code> up in a thread if I want it to block indefinitely instead of returning after a fixed timeout.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload