13

スレッド名例:'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);

}

注:現在のスレッドのスレッド名を尋ねていません。実行中のスレッドのセットからスレッド名を取得することに興味があります(上記の例を参照)。したがって、[NSThreadcurrentThread]に関する解決策は機能しません。

4

2 に答える 2

18

問題は単純です。task_threadsの配列ではなく、Machポートの配列を返しますpthread_t。への呼び出しでpthread_getname_npは、Machポートをとして扱っていpthread_tます。ただし、Machポートはではありませんpthread_tpthread_tそれぞれをusingに変換する必要がありますpthread_from_mach_thread_np

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: 
于 2013-03-14T08:22:25.510 に答える
8

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003746

[[NSThread currentThread] name]トリックを行う必要があります、私は信じています。

于 2013-03-14T04:54:12.007 に答える