NSThread setThreadPriority:不工作

NSThread setThreadPriority: is not working

我正在使用 + (BOOL)setThreadPriority:(double)p; 更改 NSThread 的优先级,但 threadPriority 始终为 0.5。 Return setThreadPriority: 的值是 TURE

_thread =  [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];

-(void)runThread {
    @autoreleasepool {
        [[NSThread currentThread] setName:@"3DF7EF974A80"];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [_condition lock];
        [_condition signal];
        [_condition unlock];
        [NSThread setThreadPriority:1.0];
        CFRunLoopRun();

        [runLoop removePort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [_condition lock];
        [_condition signal];
        [_condition unlock];
    }
}

我正在使用 Xcode 7.0.1 和 OS X 10.10.5。

setThreadPriority 在模拟器上不起作用,请在设备上检查

我无法重现您描述的行为。当我在 iOS 或 Mac OS X 上执行以下操作时,它会正确设置线程优先级:

@interface ViewController ()
{
    NSThread *_thread;
    NSCondition *_condition;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _condition = [[NSCondition alloc] init];
    _thread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
    [_thread start];
    [self performSelector:@selector(checkThreadStatus) onThread:_thread withObject:nil waitUntilDone:false];
}

- (void)checkThreadStatus {
    NSLog(@"%.2f", [[NSThread currentThread] threadPriority]);
}

- (void)runThread {
    @autoreleasepool {
        [[NSThread currentThread] setName:@"3DF7EF974A80"];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [_condition lock];
        [_condition signal];
        [_condition unlock];
        [NSThread setThreadPriority:1.0];
        CFRunLoopRun();

        [runLoop removePort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [_condition lock];
        [_condition signal];
        [_condition unlock];
    }
}

@end

我们需要 minimal, yet reproducible, example of the problem 来帮助您进一步诊断。