po [NSThread 当前线程]

发布于 2024-12-05 03:29:02 字数 245 浏览 1 评论 0原文

当我执行 po [NSThread currentThread] 时,我得到

{name = (null), num = 4}

当我向左看时,我看到: 在此处输入图像描述

看起来线程号是 6,而不是 4。另外,我们需要调用哪些属性来获取该线程无论如何?

[NSThread 当前线程].number?但不存在。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半世晨晓 2024-12-12 03:29:02

线程数几乎毫无意义。

不过,线程实例是每个线程的单例。巧合的是,您可以使用 NSThread 的地址。更好的方法是深入到 mach_* API 并从该 API 中获取线程 ID。

[NSThread currentThread] 是您所得到的唯一的数字。如果线程终止然后创建一个新线程,您可能会看到相同的地址被出售。 mach API 确实会提供一些同样独特的东西。

你想做什么?

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?

别挽留 2024-12-12 03:29:02

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

Thread.current.value(forKeyPath: "private.seqNum")!

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

Thread.current.value(forKeyPath: "private.seqNum")!
熟人话多 2024-12-12 03:29:02

这是我发布到 iOS 上的 NSThread number? 的答案:


@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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文