NSThread setThreadPriority:不起作用

问题描述:

我正在使用+ (BOOL)setThreadPriority:(double)p;更改NSThread的优先级,但threadPriority始终为0.5。返回值setThreadPriority:TURENSThread setThreadPriority:不起作用

_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。

+0

当你调用它时,它是否返回'TRUE'或'FALSE'?你是否在自己手动创建的线程上调用了它? – Rob

+0

它返回TRUE。我正在手动创建线程。我添加了代码。 –

+0

您是否尝试在运行循环运行语句之后设置线程优先级? – Astoria

我无法重现您所描述的行为。当我在任的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

SetThreadPriority函数的模拟器不能正常工作,请检查该设备上

+0

此问题适用于OS X. –