Getting thread id of current method call

IphoneObjective CCocoaMultithreading

Iphone Problem Overview


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

(objective-c please)

Iphone Solutions


Solution 1 - Iphone

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

Solution 2 - Iphone

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

Solution 3 - Iphone

In Swift 5

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

Solution 4 - Iphone

In Swift

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

Solution 5 - Iphone

In Swift4

print("\(Thread.current)")

Solution 6 - Iphone

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

Solution 7 - Iphone

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

NSLog output

Solution 8 - Iphone

uint64_t tid; pthread_threadid_np(NULL, &tid);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexi GrooveView Question on Stackoverflow
Solution 1 - IphonenallView Answer on Stackoverflow
Solution 2 - IphoneneoneyeView Answer on Stackoverflow
Solution 3 - IphonedimohamdyView Answer on Stackoverflow
Solution 4 - IphoneGlauco NevesView Answer on Stackoverflow
Solution 5 - IphoneLeanid VoukView Answer on Stackoverflow
Solution 6 - IphoneWizView Answer on Stackoverflow
Solution 7 - IphoneArtemView Answer on Stackoverflow
Solution 8 - IphonefengxingView Answer on Stackoverflow