xcode po [NSThread 当前线程]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7473258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
po [NSThread currentThread]
提问by user4951
When I do po [NSThread currentThread], I got
当我执行 po [NSThread currentThread] 时,我得到了
{name = (null), num = 4}
{名称=(空),数量= 4}
When I look to the left I see:
当我向左看时,我看到:
Looks like it's Thread number 6, not 4. Also what properties do we need to call to get that thread numbers anyway?
看起来它是线程编号 6,而不是 4。此外,我们还需要调用哪些属性来获取该线程编号?
[NSThread currentThread].number? Doesn't exist though.
[NSThread currentThread].number?虽然不存在。
回答by bbum
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 的地址。更好的是,深入到 mach_* API 并从该 API 中获取线程 ID。
[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.
[NSThread currentThread]
与您将获得的数字一样独特。如果线程终止,然后创建了一个新线程,您可能会看到出售的相同地址。mach API 将提供一些非常独特的东西,真的。
What are you trying to do?
你想做什么?
回答by nielsbot
Here's the answer I posted to NSThread number on iOS?:
这是我在 iOS 上发布到NSThread 号码的答案?:
@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