Getting thread id of current method call

后端 未结 6 637
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 14:01

Is there a way to print out the current thread id on which the current method is executing on?

(objective-c please)

相关标签:
6条回答
  • 2020-12-07 14:05
    #include <pthread.h>
    ...
    mach_port_t machTID = pthread_mach_thread_np(pthread_self());
    NSLog(@"current thread: %x", machTID);
    
    0 讨论(0)
  • 2020-12-07 14:11

    In Swift

    print("Current thread \(NSThread.currentThread())")
    
    0 讨论(0)
  • 2020-12-07 14:12
    NSLog(@"%@", [NSThread currentThread]);
    
    0 讨论(0)
  • 2020-12-07 14:17

    In Swift4

    print("\(Thread.current)")

    0 讨论(0)
  • 2020-12-07 14:21

    In Swift 5

    print("Current thread \(Thread.current)")
    
    0 讨论(0)
  • 2020-12-07 14:27

    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;
    }
    
    0 讨论(0)
提交回复
热议问题