【问题标题】:Retrieve Thread Name In IOS of non-current thread在非当前线程的 IOS 中检索线程名称
【发布时间】:2013-03-02 08:02:39
【问题描述】:

如何检索线程的“名称”?

(请参阅应用程序暂停的 xcode 图片,其中,我所说的“名称”以黄色突出显示,“com.apple.coremedia.player.async”......我可以检索正在运行的线程,并拥有尝试了以下,没有运气

mach_msg_type_number_t count, i;
thread_act_array_t list;

task_threads(mach_task_self(), &list, &count);
for (i = 0; i < count; i++) {
    if (list[i] == mach_thread_self()) continue;

    char theName[16];

    memset(theName, 0x00, sizeof(theName));
    pthread_getname_np(list[i], theName);
    printf("The thread name is %s.\n", theName);

}

注意:我不是在询问当前线程的线程名称。我有兴趣从一组正在运行的线程中获取线程名称(参见上面的示例)。所以关于 [NSThread currentThread] 的解决方案不会工作

【问题讨论】:

  • 我相信 PLCrashReporter 能够做到这一点,请尝试查看他们的页面。
  • 你到底想通过这样做来完成什么?
  • 打印出每个正在运行的线程的线程名

标签: iphone ios objective-c c multithreading


【解决方案1】:

您的问题很简单:task_threads 返回一个 Mach 端口数组,而不是一个 pthread_t 数组。在您对pthread_getname_np 的调用中,您将马赫端口视为pthread_t。但是马赫端口不是pthread_t。您需要使用pthread_from_mach_thread_np 将每个转换为pthread_t

static void dumpThreads(void) {
    char name[256];
    mach_msg_type_number_t count;
    thread_act_array_t list;
    task_threads(mach_task_self(), &list, &count);
    for (int i = 0; i < count; ++i) {
        pthread_t pt = pthread_from_mach_thread_np(list[i]);
        if (pt) {
            name[0] = '\0';
            int rc = pthread_getname_np(pt, name, sizeof name);
            NSLog(@"mach thread %u: getname returned %d: %s", list[i], rc, name);
        } else {
            NSLog(@"mach thread %u: no pthread found", list[i]);
        }
    }
}

我的测试程序的输出:

2013-03-14 03:21:45.908 hole[28315:c07] url connection complete
2013-03-14 03:21:46.787 hole[28315:c07] mach thread 3079: getname returned 0: 
2013-03-14 03:21:46.789 hole[28315:c07] mach thread 6147: getname returned 0: 
2013-03-14 03:21:46.790 hole[28315:c07] mach thread 6915: getname returned 0: 
2013-03-14 03:21:46.792 hole[28315:c07] mach thread 7683: getname returned 0: WebThread
2013-03-14 03:21:46.794 hole[28315:c07] mach thread 13059: getname returned 0: com.apple.NSURLConnectionLoader
2013-03-14 03:21:46.796 hole[28315:c07] mach thread 16131: getname returned 0: 
2013-03-14 03:21:46.798 hole[28315:c07] mach thread 17667: getname returned 0: 
2013-03-14 03:21:46.801 hole[28315:c07] mach thread 18187: getname returned 0: com.apple.CFSocket.private
2013-03-14 03:21:46.802 hole[28315:c07] mach thread 20227: getname returned 0: 

【讨论】:

  • 你先生真棒!感谢您花时间回答我的问题。社区感谢您的服务 =)
  • 如果线程名称是由某人设置的,这对我来说很好。如果线程是调度工作线程,则可以很好地找出正在该线程上执行的调度队列的名称。项目github.com/kstenerud/KSCrash 具有函数 ksmach_getThreadName 和 ksmach_getThreadQueueName 但它们在 OSX 10.9 上不适用于我。
【解决方案2】:

【讨论】:

  • 请注意,只有当 OP 只需要当前线程时,此答案才会有所帮助。如果 OP 想要所有线程,那么这个答案没有帮助。
  • 如何通过列表(而不是当前线程)获取线程名称,以获取线程名称。如上面的示例 task_threads(mach_task_self(), &list, &count) ;
  • 这会为我返回一个空字符串。
  • @bugloaf 那是因为它们没有为您的线程设置名称,您可以像这样设置线程的名称:[[NSThread currentThread] setName:@"my thread"]。或者在启动线程之前将其命名为[thread setName:@"custom thread"]
猜你喜欢
  • 1970-01-01
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多