简体   繁体   中英

sysCall(Sys_gettid) return -1

I use sysCall(Sys_gettid) on iOS app, but return -1.

const char *elog_port_get_t_info(void) {
    static char cur_thread_info[10] = { 0 };

    snprintf(cur_thread_info, 10, "tid:%04ld", syscall(SYS_gettid));

    return cur_thread_info;
}

You shouldn't use syscall on iOS or macOS directly. The gettid call is meant to get the current thread ID. Depending on what you want to do there are several alternatives:

For example, you can query the thread ID using pthreads:

#include <pthread.h>
uint64_t tid;
pthread_threadid_np(NULL, &tid);

You could also make use of NSThread . Either use the pointer returned by [NSThread currentThread] or, probably better, make use of the threadDictionary :

(untested)
NSString * identifier = thread.threadDictionary[@"myIDKey"];
if (identifier == nil) {
   identifier = [NSUUID UUID].UUIDString;
   thread.threadDictionary["myIDKey"] = identifier;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM