133

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

(objective-c please)

nall
  • 15,899
  • 4
  • 61
  • 65
Alexi Groove
  • 6,646
  • 12
  • 47
  • 54
  • 4
    nall answered the question, but not the real question.... why do you want to know? Beyond debugging or asserting correct behavior, basic stuff on currentThread is *generally* a bad idea. – bbum Oct 24 '09 at 02:15
  • 1
    I for one needed it to create thread-local objects that are attached to another object (i.e. associated to a parent object _and_ thread -- not just the thread). – adib Dec 15 '10 at 07:01

8 Answers8

243
NSLog(@"%@", [NSThread currentThread]);
nall
  • 15,899
  • 4
  • 61
  • 65
  • What is the meaning of name = (null), if thread is main it returns NSThread: 0x60800006cb80>{number = 1, name = main}, Does it means "name = (null)" refers to background thread. – Nirmala Maurya Aug 24 '17 at 04:59
  • 1
    And how does one grab that name and number? `name` returns empty description even for main and `number` is nowhere to be found – Hari Honor Oct 17 '17 at 11:24
  • 1
    Quick and dirty thread num: `NSString *s = [NSString stringWithFormat:@"%@", [NSThread currentThread]]; int threadNum = -1; sscanf(s.UTF8String, "{number = %d", &threadNum);` – Hari Honor Oct 17 '17 at 11:47
38

In Swift 5

print("Current thread \(Thread.current)")
dimohamdy
  • 2,917
  • 30
  • 28
36
#include <pthread.h>
...
mach_port_t machTID = pthread_mach_thread_np(pthread_self());
NSLog(@"current thread: %x", machTID);
neoneye
  • 50,398
  • 25
  • 166
  • 151
  • 4
    @Rajneesh071 Indeed, what else did you expect, display the ID of a different thread? – meaning-matters Mar 13 '14 at 08:45
  • The `[NSThread currentThread]` class method returns an NSThread object. You can log the address of that thread object, but how do you get the numeric thread ID, like what's displayed in the Xcode debugger? (Thread 1 for the main thread, and increasing numbers for each additional thread that's created by your app.) – Duncan C Oct 02 '15 at 12:43
  • @Duncan: That's almost certainly a feature of the debugger, not the OS/runtime. – Cameron Dec 15 '15 at 18:29
13

In Swift

print("Current thread \(NSThread.currentThread())")
Glauco Neves
  • 3,483
  • 1
  • 24
  • 36
7

In Swift4

print("\(Thread.current)")
Paul Hildebrandt
  • 2,724
  • 28
  • 26
Leanid Vouk
  • 460
  • 6
  • 13
3

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;
}
Wiz
  • 474
  • 4
  • 11
2

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

NSLog output

Artem
  • 373
  • 2
  • 6
0

uint64_t tid; pthread_threadid_np(NULL, &tid);

fengxing
  • 374
  • 1
  • 4
  • 10