Alexi Groove
Alexi Groove

Reputation: 6686

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)

Upvotes: 135

Views: 70380

Answers (8)

fengxing
fengxing

Reputation: 474

uint64_t tid; pthread_threadid_np(NULL, &tid);

Upvotes: 0

Artem
Artem

Reputation: 411

NSLog prints to the console a number (in square brackets after the colon) identifying the thread on which it was called.

NSLog output

Upvotes: 3

Leanid Vouk
Leanid Vouk

Reputation: 508

In Swift4

print("\(Thread.current)")

Upvotes: 8

dimohamdy
dimohamdy

Reputation: 3053

In Swift 5

print("Current thread \(Thread.current)")

Upvotes: 40

Glauco Neves
Glauco Neves

Reputation: 3537

In Swift

print("Current thread \(NSThread.currentThread())")

Upvotes: 13

Wiz
Wiz

Reputation: 474

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;
}

Upvotes: 3

neoneye
neoneye

Reputation: 52231

#include <pthread.h>
...
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"current thread: %x", machTID);

Upvotes: 38

nall
nall

Reputation: 16149

NSLog(@"%@", [NSThread currentThread]);

Upvotes: 245

Related Questions