gpt4 book ai didi

iphone - 在非当前线程的 IOS 中检索线程名称

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:06 24 4
gpt4 key购买 nike

Thread name Example: 'com.apple.coremedia.player.async'

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

(请参阅应用程序暂停的 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] 的解决方案不会工作

最佳答案

您的问题很简单:task_threads 返回 Mach 端口数组,而不是 pthread_t 数组。在调用 pthread_getname_np 时,您将 Mach 端口视为 pthread_t。但是 Mach 端口不是 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:

关于iphone - 在非当前线程的 IOS 中检索线程名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com