P:\test>512584
1 started h(100)
2 started h(124)
3 started h(136)
4 started h(148)
5 started h(172)
6 started h(184)
7 started h(196)
tid(7) priority(15) received 2000000 timeslices
tid(6) priority(2) received 2000000 timeslices
tid(5) priority(1) received 2000000 timeslices
tid(4) priority(0) received 342158 timeslices
tid(3) priority(-1) received 3 timeslices
tid(2) priority(-2) received 2 timeslices
tid(1) priority(-15) received 2 timeslices
####
P:\test>512584
1 started h(104)
2 started h(132)
3 started h(144)
4 started h(156)
5 started h(180)
6 started h(192)
7 started h(204)
tid(2) priority(-2) received 5087 timeslices
tid(1) priority(-15) received 5086 timeslices
tid(7) priority(15) received 5121 timeslices
tid(6) priority(2) received 5120 timeslices
tid(5) priority(1) received 5117 timeslices
tid(4) priority(0) received 5114 timeslices
tid(3) priority(-1) received 5099 timeslices
####
#! perl -slw
use strict;
use threads;
use threads::shared;
use Win32::API;
$| = 1;
Win32::API->Import( 'Kernel32', q[
BOOL DuplicateHandle( HANDLE hSourceProcessHandle,
HANDLE hSourceHandle, HANDLE hTargetProcessHandle,
LPHANDLE lpTargethandle, DWORD dwDesiredAccess,
BOOL bInheritHandle, DWORD dwOptions )
] ) or die $^E;
Win32::API->Import( 'Kernel32', q[ HANDLE GetCurrentProcess() ] ) or die $^E;
Win32::API->Import( 'Kernel32', q[ HANDLE GetCurrentThread() ] ) or die $^E;
Win32::API->Import( 'Kernel32',
q[ BOOL SetThreadPriority( HANDLE hThread, int nPriority ) ] ) or die $^E;
Win32::API->Import( 'Kernel32',
q[ int GetThreadPriority( HANDLE hThread ) ] ) or die $^E;
my %threads : shared;
my $running : shared = 0;
my $go : shared = 0;
sub thread{
my $tid = threads->self->tid;
my $hProc = GetCurrentProcess();
my $hThread;
DuplicateHandle( $hProc, GetCurrentThread(), $hProc, $hThread, 0, 0, 2 );
$threads{ $tid } = $hThread;
print "$tid started h($hThread)";
++$running;
my $count = 0;
Win32::Sleep 1 until $go;
for ( 1 .. 2_000_000 ) {
$count++ and Win32::Sleep 1;
last if !$go;
}
printf "tid($tid) priority(%d) received $count timeslices\n",
GetThreadPriority( GetCurrentThread() );
--$running;
}
my @priorities = ( -15, -2, -1, 0, 1, 2, +15 );
my @threads = map{ threads->create( \&thread ) } 0 .. $#priorities;
sleep 1 until $running == @threads;
SetThreadPriority( $threads{ $_ + 1 }, $priorities[ $_ ] )
or die $^E for 0 .. $#priorities;
$go = 1;
sleep 10;
$go = 0;
sleep 1 while $running;