Getting thread id of current method call
Is there a way to print out the current开发者_运维技巧 thread id on which the current method is executing on?
(objective-c please)
NSLog(@"%@", [NSThread currentThread]);
In 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);
In Swift
print("Current thread \(NSThread.currentThread())")
In Swift4
print("\(Thread.current)")
you can hack something up like this (this just prints pretty, but you can go ahead and split until you get the number):
+ (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;
}
NSLog
prints to the console a number (in square brackets after the colon) identifying the thread on which it was called.
uint64_t tid; pthread_threadid_np(NULL, &tid);
精彩评论