Objective-C 获取线程ID

BOOL getIvarValue(id instance, const char *name, size_t size, void *buffer)
{
    Ivar thisIvar = class_getInstanceVariable([instance class], name);
    ptrdiff_t offset = ivar_getOffset( thisIvar );
    if (0 == offset) return NO;
    
    void *ptr = FBridge(instance, id, void*) + offset;
    memset(buffer, 0, size);
    memcpy(buffer, ptr, size);
    return YES;
}

- (int)seqNumber
{
    __unsafe_unretained NSObject *td = nil;
    getIvarValue(self, "_private", sizeof(td), &td);
    
    int tid = 0;
    getIvarValue(td, "seqNum", sizeof(tid), &tid);
    return tid;
}

你可能感兴趣的:(Objective-C 获取线程ID)