开发者

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.

Getting thread id of current method call


uint64_t tid; pthread_threadid_np(NULL, &tid);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜