更改线程优先级ERROR_INVALID_HANDLE

时间:2014-12-03 13:31:58

标签: multithreading perl winapi thread-priority

我试图在我的脚本中更改线程优先级,但没有成功,这里有详细信息。

$thr = threads->new(\&someFunction, 
                    $shared variable 1,
                    $shared variable 2, 
                   );

我已尝试使用threads::State;

$thr->priority(2);

没有成功

所以,我认为Win32::API必须正常工作

my $functionGetLastError= Win32::API->new('Kernel32', 
                                          'GetLastError',
                                          '', 
                                          'N'
                                       );
my $functionSetThreadPriority= Win32::API->new('Kernel32', 
                                               'SetThreadPriority',
                                               'II', # I've tried 'PI' and 'II' as well
                                               'N'
                                              );
my $h = $thr->_handle();
my $success = $functionSetThreadPriority->Call( $h, 2 );
warn "Return Error #".$functionGetLastError->Call() if !$success;

再次,没有成功:(,但现在我有一个线索,脚本返回错误号

  

上次错误6

MSDN site, System Error Codes (0-499)开始,错误似乎是

  

ERROR_INVALID_HANDLE

我做错了什么?

2 个答案:

答案 0 :(得分:3)

$thread->_handle奇怪地返回HANDLE*,而SetThreadPriority则需要HANDLE。您需要取消引用指针,您可以执行以下操作:

use constant THREAD_PRIORITY_HIGHEST => 2;

sub SetThreadPriority {
   my ($thread, $priority) = @_;

   # $thread->_handle() returns a HANDLE*.
   my $handle_ptr    = $thread->_handle();
   my $packed_handle = unpack('P'.HANDLE_SIZE, pack(PTR_FORMAT, $handle_ptr));
   my $handle        = unpack(HANDLE_FORMAT, $packed_handle);

   state $SetThreadPriority = (
      Win32::API->new('Kernel32', 'SetThreadPriority', 'Ni', 'i')
         or die("Loading SetThreadPriority: $^E\n")
   );

   return $SetThreadPriority->Call($handle, $priority);
}

这是完整的测试程序:

use strict;
use warnings;
use feature qw( say state );

use threads;
use threads::shared;

use Carp       qw( croak );
use Config     qw( %Config );
use Win32::API qw( );

sub uint_format {
     $_[0] == 4 ? 'L'
   : $_[0] == 8 ? 'Q'
   : croak("Unsupported")
}

use constant PTR_SIZE   => $Config{ptrsize};
use constant PTR_FORMAT => uint_format(PTR_SIZE);

use constant HANDLE_SIZE   => PTR_SIZE;
use constant HANDLE_FORMAT => PTR_FORMAT;

use constant THREAD_PRIORITY_HIGHEST => 2;

sub SetThreadPriority {
   my ($thread, $priority) = @_;

   # $thread->_handle() returns a HANDLE*.
   my $handle_ptr    = $thread->_handle();
   my $packed_handle = unpack('P'.HANDLE_SIZE, pack(PTR_FORMAT, $handle_ptr));
   my $handle        = unpack(HANDLE_FORMAT, $packed_handle);

   state $SetThreadPriority = (
      Win32::API->new('Kernel32', 'SetThreadPriority', 'Ni', 'i')
         or die("Loading SetThreadPriority: $^E\n")
   );

   return $SetThreadPriority->Call($handle, $priority);
}

{
   my $done :shared = 0;

   my $thread = async {
      { lock($done); cond_wait($done) while !$done; }
   };

   my $rv = SetThreadPriority($thread, THREAD_PRIORITY_HIGHEST);
   say $rv ? "Success" : "Error: $^E";

   { lock($done); $done = 1; cond_broadcast($done); }
   $thread->join();
}

请注意,您可以使用$^E访问GetLastError

SetThreadPriority($handle, THREAD_PRIORITY_HIGHEST)
   or die("SetThreadPriority: $^E\n";

答案 1 :(得分:0)

  

ERROR_INVALID_HANDLE

这表明_handle返回的内容不是Win32 :: API所理解的。我怀疑" P"想要一个字符串缓冲区而不是一个整数转换指针。 " I"可能是错误的,因为它在64位上的大小错误,我会尝试" N"自己。

此外,对于将来在Unix上遇到此问题的读者:尝试我的POSIX::RT::Scheduler模块。