Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
I see that my approach has been totally bum-over-tits so I consider this case closed :)

Hm. Just because the initial approach is bad, it doesn't mean that the over all goal is unobtainable.

Eg. This almost does what you asked for (note the Inline::C bit is only to get accurate timings; it doesn't affect what the code does):

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'rdtsc', CLEAN_AFTER_BUILD => 0; SV *rdtsc() { return newSVuv( (UV)__rdtsc() ); } END_C use threads; use threads::shared; my $sig:shared; my $l0 :shared; my $l1 :shared; my $ts :shared = 0; async( sub { my $lockRef = shift; my $tid = threads ->tid; while( 1 ) { printf "[%5d] Waiting at: %10u ms\n", $tid, ( rdtsc() - $ts + ) / 2400; { lock $$lockRef; cond_wait( $sig, $$lockRef ); } printf "[%5d] Got signal at: %10u ms\n", $tid, ( rdtsc() - $ts + ) / 2400; } }, $_ )->detach for \$l0, \$l1; sleep 1; ## Give threads a chance to start. while( 1 ) { { lock $ts; $ts = rdtsc() }; printf "[main] signalling at: %10u\n", 0; { lock $sig; cond_broadcast( $sig ) }; sleep 3; } __END__ C:\test>cond_b.pl [ 1] Waiting at: 7250756853 ms [ 2] Waiting at: 7250765890 ms [main] signalling at: 0 [ 2] Got signal at: 1422 ms [ 1] Got signal at: 1426 ms [ 2] Waiting at: 3361 ms [ 1] Waiting at: 6105 ms [main] signalling at: 0 [ 2] Got signal at: 1384 ms [ 1] Got signal at: 1398 ms [ 2] Waiting at: 3695 ms [ 1] Waiting at: 7011 ms [main] signalling at: 0 [ 2] Got signal at: 1362 ms [ 1] Got signal at: 1361 ms [ 2] Waiting at: 3672 ms [ 1] Waiting at: 7077 ms Terminating on signal SIGINT(2)

NB: ms above is microseconds!

But note that even on my 4 core system, the 2 threads do not see the signal at exactly the same time. (also, if you leave it running, you'll see one of the limitations (a.k.a total idiocies) of the cond_var mechanism.

But that brings me back to the questions I alluded to earlier. Why have your main thread attempt to coordinate starting two other threads; and then just sit there doing nothing? Ie, why use 3 threads?

I can see that you might need to run the recorder and player from different threads, but why not:

main: start recorder thread; main: start player (in main); main: wait for player to finish; main: signal recorder thread to stop.

This mirrors what you'd do manually. Switch the recorder on; switch the player on; when the player finishes; switch the recorder off.

There is plenty of scope for solving the problem; you just need to tackle it the right way.


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^2: Synchronising threads with signals (or not) by BrowserUk
in thread Synchronising threads with signals (or not) by forestcreature

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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 taking refuge in the Monastery: (4)
    As of 2015-07-05 08:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (61 votes), past polls