1

I am using + (BOOL)setThreadPriority:(double)p; to change priority of NSThread but threadPriority is always 0.5. Return value of setThreadPriority: is 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];
    }
}

I am using Xcode 7.0.1 and OS X 10.10.5.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • When you called this, did it return `TRUE` or `FALSE`? Did you call this on a thread you manually created yourself? – Rob Nov 17 '15 at 05:23
  • it return TRUE. I am creating thread manually. I have added code. – Parag Bafna Nov 17 '15 at 05:39
  • did you try to set thread priority after run loop run statement? – Daniyar Nov 17 '15 at 08:19
  • @Astoria that will get called only if you stop _thread. – Parag Bafna Nov 17 '15 at 10:43
  • Any difference if you do `[[NSThread currentThread] setThreadPriority:1.0]`? Perhaps the class method is not working properly? – bhaller Nov 17 '15 at 14:52
  • I just tested this code and it set the priority fine. Where are you checking the `threadPriority`? Are you checking it from within that thread? Or are you checking it from the main thread, perhaps immediately after you instantiated/started this secondary thread (in which case `runThread` may not have run yet. – Rob Nov 18 '15 at 06:14
  • @Rob I am calling function on runThread using - (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(nullable id)arg waitUntilDone:(BOOL)wait and checking priority inside aSelector – Parag Bafna Dec 22 '15 at 10:55

2 Answers2

0

setThreadPriority doesn't work on simulator, please check this on device

Divyanshu Sharma
  • 551
  • 2
  • 12
0

I cannot reproduce the behavior you describe. When I do the following on either iOS or Mac OS X, it correctly sets the thread priority:

@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

We need minimal, yet reproducible, example of the problem in order to help you diagnose this further.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044