Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

First off, you are probably limiting your audience unnecessarily by mentioning "threads" in the title.

Your basic problem, that of needing a non-blocking read, has nothing to do with threading.

Secondly, do you really need to terminate your read thread? That is, if you detached it, it would just silently melt into the ether when your main thread decides to exit the process.

But if you do really need to, I think that you've already discovered the 'right' solution, namely read_bg(). All you nee to do is use the API correctly and alter your coding strategy a little. (NOTE: None of the following is tested.)

So I changed to "background read" my ($receivedBytes, $data) = $HandleToRS232->read_bg(); Doing this I got nothing in $data so I did a call to my($done, $count_in, $string_in) = $HandleToRS232->read_done(1); Then I receive the message in the $string_in variable, but the call to read_done is also blocking.

Based on my reading the source -- and I may well have misread it --

  • read_bg() uses overlapped IO to operate, therefore it is expected that it will (often) not return (somplete) data immediately

    It basically says, this is what I want to read, initiate that for me and return immediately (with any data that happens to have already arrived.)

    I'll call read_done() when I'm ready for (the rest of) it.

  • read_done() is only blocking if you pass a true argument (As you are:read_done(1);).

    To prevent it from blocking, pass 0;

    That will require you to recode your read thread so that instead of blocking in a read, you poll (with a delay!).

    Then within that polling loop you can check to see if you should exit the thread.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Threaded RS232 communication by BrowserUk
in thread Threaded RS232 communication by tobias_hofer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2014-07-31 20:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (253 votes), past polls