it-source

현재 메서드 호출의 스레드 ID를 가져오는 중

criticalcode 2023. 5. 21. 11:36
반응형

현재 메서드 호출의 스레드 ID를 가져오는 중

현재 메서드가 실행 중인 현재 스레드 ID를 출력할 수 있는 방법이 있습니까?

(209-c로 부탁합니다)

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

인 스위프트 5

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

인 스위프트

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

스위프트4

print("\(Thread.current)")

다음과 같은 것을 해킹할 수 있습니다(이것은 단지 예쁘게 인쇄되지만, 번호를 얻을 때까지 계속해서 나눌 수 있습니다).

+ (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호출된 스레드를 식별하는 숫자(콜론 뒤의 대괄호로 표시)를 콘솔에 인쇄합니다.

NSLog 출력

unint64_ttid; pthread_threadid_np(NULL, &tid);

언급URL : https://stackoverflow.com/questions/1616572/getting-thread-id-of-current-method-call

반응형