Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
Thread.Yield();
busywork();
If this method succeeds, the rest of the thread's current time slice is yielded. The operating system schedules the calling thread for another time slice, according to its priority and the status of other threads that are available to run.
Thread.yield();
busywork();
This is just a hint to the scheduler. The doc states "It is rarely appropriate to use this method".
sub busywork { print 'busy' };
my $n = 12;
my $th_sub = sub {
my ($limit) = @_;
foreach ( 1 .. $limit ) {
print '.';
cede;
}
};
my $th = Coro->new( $th_sub, $n );
$th->ready;
foreach my $ii ( 1 .. $n ) {
if ( $ii == int $n * 2/3 ) {
$Coro::current->prio(-1);
cede;
busywork();
}
print '-';
cede;
}
CPAN module Coro provides threading support. Here we create a thread and put it in the ready queue. The main thread prints a dash then cedes (yields) control and thread $th takes over and prints a period. This alternates back and forth until 2/3rds of the limit is reached, at which point the main thread is lowered in priority and cedes control before calling busywork(). For $n == 12 the net result is the string -.-.-.-.-.-.-......busy-----.
::std::thread::yield_now();
busywork();