multithreading - 更改线程优先级ERROR_INVALID_HANDLE

标签 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;

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

last Error 6



MSDN site, System Error Codes (0-499)看来,错误是

ERROR_INVALID_HANDLE



我究竟做错了什么?

最佳答案

$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";

关于multithreading - 更改线程优先级ERROR_INVALID_HANDLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27273020/

相关文章:

Perl 序列化和反序列化散列的散列

regex -\x 在替换中不起作用

.net - .NET 的 SuppressKeypress 如何/为何工作?

java - 最终静态与非最终静态变量?

java - Netty 客户端相对于 I/O 客户端的优势?

Spring Data JPA - 返回 Future

perl - 更改 map 返回的上下文?

winapi - WindowsFromDc 返回 null

c++ - 在C/C++中滚动自己的键盘/输入系统

c++ - Windows 操作系统中的线程安全和原子读取