gpt4 book ai didi

objective-c - NSThread setThreadPriority : is not working

转载 作者:行者123 更新时间:2023-12-03 17:19:57 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com