#! 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;