objective-c - NSThread setThreadPriority : is not working

标签 objective-c macos cocoa nsthread

我使用+ (BOOL)setThreadPriority:(double)p;来更改NSThread的优先级,但threadPriority始终为0.5。 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。

最佳答案

我无法重现您所描述的行为。当我在 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以帮助您进一步诊断。

关于objective-c - NSThread setThreadPriority : is not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33749541/

相关文章:

objective-c - 带有文本附件和截断的属性字符串

c++ - 无法运行使用 macdeployqt 创建的 OSX 应用程序。使用 Fusion 样式时缺少 Cocoa 插件

cocoa - NSarray 发布

cocoa - 使用 Cocoa (OS X) 进行内存中 mime 类型检测?

iphone - 在 iPhone 的照片库中写入 UIImage 以及元数据(EXIF、GPS、TIFF)

ios - 应用程序崩溃,说集合表达式类型UIView *'可能不响应'countByEnumeratingWithState:object:count“'

Windows 上 Mac 的 Python 交叉编译

xcode - 有人能解释一下如何将 Chromium 包含在现有的 XCode 项目中吗?

objective-c - iPad UIModalPresentationPageSheet 前 40 像素被截掉

macos - Cocoa:循环遍历窗口中的所有控件?