Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^5: The most precise second (timer)

by misc (Friar)
on Dec 04, 2019 at 12:14 UTC ( [id://11109655]=note: print w/replies, xml ) Need Help??


in reply to Re^4: The most precise second (timer)
in thread The most precise second (timer)

..Again, in pseudocode, this would work:
sub threadwork(){ lock(MUTEX); do{ the work; } } $t = new threadpool(42 threads); $t->do( &threadwork() ); lock(MUTEX); loop{ ct = timestamp(); sleep(0.9 seconds); # do a blocking select with timeout in perl fo +r microsecond timings loop {nop;} while (ct==timestamp()); # the spinloop #or, for a interval of 5 seconds: while ( ct+5>timestamp() ); unlock(MUTEX); }

Hope that helps

Replies are listed 'Best First'.
Re^6: The most precise second (timer - MCE demonstrations)
by marioroy (Prior) on Dec 04, 2019 at 19:57 UTC

    Hello! I have been following this thread. MCE has yield.

    Today, released MCE 1.864 and MCE::Shared 1.864 to use CLOCK_MONOTONIC, if available. Yield gives other workers a chance to run, but helpful to throttle workers as well. Due to the serial nature, yield can also be used to do something periodically.

    use strict; use warnings; use MCE::Child 1.864; sub task { while () { MCE::Child->yield(1.0); my $time = MCE::Util::_time(); printf "pid $$, time %12.06f\n", $time; } } MCE::Child->create(\&task) for 1..5; MCE::Child->wait_all;

    Output from a Linux VM.

    pid 3245, time 151.518361 pid 3246, time 152.518277 pid 3248, time 153.518325 pid 3247, time 154.518268 pid 3249, time 155.518331 pid 3245, time 156.518396 pid 3246, time 157.518181 pid 3248, time 158.518237 pid 3247, time 159.518258 pid 3249, time 160.518230 pid 3245, time 161.518247 pid 3246, time 162.518074 pid 3248, time 163.518094 pid 3247, time 164.518285 pid 3249, time 165.518338 pid 3245, time 166.518081 pid 3246, time 167.518232 pid 3248, time 168.518047 pid 3247, time 169.518250 pid 3249, time 170.518334 pid 3245, time 171.518336 pid 3246, time 172.518232 pid 3248, time 173.518244 pid 3247, time 174.518247 pid 3249, time 175.518338 pid 3245, time 176.518231 pid 3246, time 177.518231 pid 3248, time 178.518238 pid 3247, time 179.518238 pid 3249, time 180.518334 pid 3245, time 181.518237 pid 3246, time 182.518384 pid 3248, time 183.518225 pid 3247, time 184.518203 pid 3249, time 185.518220 pid 3245, time 186.518086 pid 3246, time 187.518264 pid 3248, time 188.518229 pid 3247, time 189.518248 pid 3249, time 190.518250 ...

    Yield now works similarly for the manager process starting with 1.864. This allows the manager to send work periodically to a pool of workers.

    use strict; use warnings; use MCE::Child 1.864; use MCE::Channel; my $chnl = MCE::Channel->new( impl => 'Mutex' ); sub task { while ( my $work = $chnl->recv() ) { my $time = MCE::Util::_time(); printf "pid $$, time %12.06f\n", $time; } } MCE::Child->create(\&task) for 1..5; for ( 1..40 ) { MCE::Child->yield(1.0); $chnl->send("work $_"); } $chnl->end(); MCE::Child->wait_all();

    Output from a Linux VM.

    pid 9336, time 2111.790429 pid 9340, time 2112.790575 pid 9337, time 2113.790593 pid 9340, time 2114.790489 pid 9338, time 2115.790415 pid 9339, time 2116.790520 pid 9338, time 2117.790471 pid 9336, time 2118.790468 pid 9337, time 2119.790466 pid 9340, time 2120.790467 pid 9338, time 2121.790340 pid 9339, time 2122.790540 pid 9338, time 2123.790289 pid 9340, time 2124.790484 pid 9337, time 2125.790351 pid 9339, time 2126.790493 pid 9338, time 2127.790459 pid 9340, time 2128.790489 pid 9336, time 2129.790488 pid 9339, time 2130.790487 pid 9338, time 2131.790488 pid 9340, time 2132.790271 pid 9336, time 2133.790449 pid 9337, time 2134.790461 pid 9339, time 2135.790462 pid 9338, time 2136.790445 pid 9340, time 2137.790494 pid 9337, time 2138.790389 pid 9339, time 2139.790528 pid 9336, time 2140.790488 pid 9340, time 2141.790337 pid 9338, time 2142.790339 pid 9340, time 2143.790253 pid 9336, time 2144.790396 pid 9337, time 2145.790544 pid 9340, time 2146.790390 pid 9336, time 2147.790411 pid 9338, time 2148.790463 pid 9339, time 2149.790362 pid 9337, time 2150.790411

    Ditto for MCE::Hobo.

    Regards, Mario

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109655]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found