当我使用po NSThread currentThread时,我得到了
{name = (null),num = 4}
当我向左看时,我看到:

看起来它是线程编号6,而不是4。另外,我们需要调用什么属性来获取线程编号呢?
NSThread currentThread.number?但并不存在。
发布于 2011-09-20 00:14:35
线程数几乎是没有意义的。
不过,线程实例是每个线程的一个单例。巧合的是,您可以使用NSThread的地址。不过,更好的做法是深入了解mach_*应用程序接口,并从该应用程序接口中获取线程ID。
[NSThread currentThread]是你能得到的最唯一的数字。如果线程终止,然后创建了一个新线程,您可能会看到相同的地址。mach API将提供一些独一无二的东西,真的。
你想做什么?
发布于 2021-05-15 06:35:38
对于Swift 5:它是一个私有变量,但可以使用keypath直接访问:
Thread.current.value(forKeyPath: "private.seqNum")!发布于 2014-05-05 12:04:41
以下是我在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 ;
}
@endhttps://stackoverflow.com/questions/7473258
复制相似问题