3

When I do po [NSThread currentThread], I got

{name = (null), num = 4}

When I look to the left I see: enter image description here

Looks like it's Thread number 6, not 4. Also what properties do we need to call to get that thread numbers anyway?

[NSThread currentThread].number? Doesn't exist though.

user4951
  • 32,206
  • 53
  • 172
  • 282
  • I found `(lldb) thread info` more useful. You could use the `tid` to connect output from Console.app / NSLog back to the thread. `thread #9: tid = 0x2e77, 0x0000000105293c9c` – rustyMagnet May 11 '19 at 04:33

3 Answers3

8

Thread numbers are meaningless, pretty much.

The thread instance, though, is a singleton per thread. You could use the NSThread's address, by coincidence. Better, still, would be to dip down to the mach_* API and grab the thread ID from that API.

[NSThread currentThread] is about as unique of a number as you'll get. If the thread terminates and then a new thread is created, you might see the same address vended. The mach APIs will vend something just about as unique, really.

What are you trying to do?

bbum
  • 162,346
  • 23
  • 271
  • 359
  • If you need per-thread data, I think what you want is either `-[NSThread threadDictionary]`, or (at a lower level) `pthread_getspecific()`/`pthread_setspecific()`. – Jonathan Grynspan Sep 20 '11 at 03:49
  • 1
    Yup -- that'd be the way to go if you want per-thread data. If you are trying to uniquely identify a thread, I might recommend generating a UUID and sticking it in the thread's dictionary. That way, when a thread is exited and a new thread created, there is no risk of collision. – bbum Sep 20 '11 at 04:15
2

For Swift 5: It's a private variable, but accessible directly using keypath:

Thread.current.value(forKeyPath: "private.seqNum")!
The Camster
  • 1,850
  • 1
  • 20
  • 27
-2

Here's the answer I posted to NSThread number on iOS?:


@implementation NSThread (ThreadGetIndex)

-(NSInteger)getThreadNum
{
    NSString * description = [ self description ] ;
    NSArray * keyValuePairs = [ description componentsSeparatedByString:@"," ] ;
    for( NSString * keyValuePair in keyValuePairs )
    {
        NSArray * components = [ keyValuePair componentsSeparatedByString:@"=" ] ;
        NSString * key = components[0] ;
        key = [ key stringByTrimmingCharactersInSet:[ NSCharacterSet whitespaceCharacterSet ] ] ;
        if ( [ key isEqualToString:@"number" ] || [ key isEqualToString:@"num" ] )
        {
            return [ components[1] integerValue ] ;
        }
    }
    @throw @"couldn't get thread num";
    return -1 ;
}

@end
Community
  • 1
  • 1
nielsbot
  • 15,922
  • 4
  • 48
  • 73
  • 3
    WARNING: It seems in iOS8 "num" has become "number" (and "number" and "name" have swapped orders... though the above code is fine for that) – Dave Owens Sep 13 '14 at 03:16
  • Useless answer, this method is only parsing the [NSThread description] and extracting the "number" value, the @Jim Thio question is completely different – Kappe May 13 '15 at 14:06
  • @DaveOwens Thanks, I changed the snippet – nielsbot May 13 '15 at 17:40