首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >po [NSThread currentThread]

po [NSThread currentThread]
EN

Stack Overflow用户
提问于 2011-09-19 23:21:18
回答 3查看 7K关注 0票数 2

当我使用po NSThread currentThread时,我得到了

{name = (null),num = 4}

当我向左看时,我看到:

看起来它是线程编号6,而不是4。另外,我们需要调用什么属性来获取线程编号呢?

NSThread currentThread.number?但并不存在。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-20 00:14:35

线程数几乎是没有意义的。

不过,线程实例是每个线程的一个单例。巧合的是,您可以使用NSThread的地址。不过,更好的做法是深入了解mach_*应用程序接口,并从该应用程序接口中获取线程ID。

[NSThread currentThread]是你能得到的最唯一的数字。如果线程终止,然后创建了一个新线程,您可能会看到相同的地址。mach API将提供一些独一无二的东西,真的。

你想做什么?

票数 8
EN

Stack Overflow用户

发布于 2021-05-15 06:35:38

对于Swift 5:它是一个私有变量,但可以使用keypath直接访问:

代码语言:javascript
运行
复制
Thread.current.value(forKeyPath: "private.seqNum")!
票数 1
EN

Stack Overflow用户

发布于 2014-05-05 12:04:41

以下是我在NSThread number on iOS?上发布的答案

代码语言:javascript
运行
复制
@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
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7473258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档