2015-11-17 28 views
1

我正在使用+ (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

回答

0

我無法重現您所描述的行爲。當我在任的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

0

SetThreadPriority函數的模擬器不能正常工作,請檢查該設備上

+0

此問題適用於OS X. –