有没有一种方法可以在当前方法执行的线程上打印出当前线程id?
(请使用Objective-C语言)
(请使用Objective-C语言)
NSLog(@"%@", [NSThread currentThread]);
name
返回空描述,而number
也无处可寻。 - Hari HonorNSString *s = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; int threadNum = -1; sscanf(s.UTF8String, "<NSThread: 0x%*12[0-9a-f]>{number = %d", &threadNum);
- Hari Honor在Swift 5中
print("Current thread \(Thread.current)")
#include <pthread.h>
...
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"current thread: %x", machTID);
[NSThread currentThread]
类方法返回一个 NSThread 对象。你可以记录该线程对象的地址,但是如何获取数字线程 ID,就像在 Xcode 调试器中显示的那样呢?(主线程为 Thread 1,每个由你的应用程序创建的附加线程都会增加编号。) - Duncan C在Swift中
print("Current thread \(NSThread.currentThread())")
print("\(Thread.current)")
+ (NSString *)getPrettyCurrentThreadDescription {
NSString *raw = [NSString stringWithFormat:@"%@", [NSThread currentThread]];
NSArray *firstSplit = [raw componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"{"]];
if ([firstSplit count] > 1) {
NSArray *secondSplit = [firstSplit[1] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"}"]];
if ([secondSplit count] > 0) {
NSString *numberAndName = secondSplit[0];
return numberAndName;
}
}
return raw;
}
uint64_t tid; pthread_threadid_np(NULL, &tid);
uint64_t tid; pthread_threadid_np(NULL,&tid);