4

I want to get the number of threads which are 'alive' in my iOS application.

Can I use threadDictionary in the NSThread class? Or can I use mach/thread_info.h?

deltaaruna
  • 528
  • 6
  • 24
  • 3
    Why do you need that information? – Martin R Jan 31 '14 at 10:55
  • Stale as soon as returned. – Martin James Jan 31 '14 at 12:14
  • 1
    @MartinR Why do you need to know, why does he need this information for? ;-) – Lukasz Apr 27 '15 at 11:46
  • @Lukasz: Because it could have been an ["XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) :) – Martin R Apr 27 '15 at 11:54
  • 1
    @MartinR OK, but his question is clear and even if it is XY something - it does not matter - because solution exists and can still be useful to others (like me, if it was few weeks ago ;-) – Lukasz Apr 27 '15 at 14:04
  • @Lukasz: I did not say that the question is unclear or bad. I just left a comment asking why (s)he needs the information, that's all. I can see no problem with that. – Martin R Apr 27 '15 at 14:06
  • This is useful if you want to know if you are the only thread in the process. That never goes stale once True until you spawn more. – gps Jan 23 '22 at 21:26

3 Answers3

7

Michael Dautermann already answered the question, but this is an example to get threads count using Mach API. Note that its work only on simulator (tested with iOS 6.1), running it on device will fail because task_for_pid return KERN_FAILURE.

/**
 * @return -1 on error, else the number of threads for the current process
 */
static int getThreadsCount()
{
    thread_array_t threadList;
    mach_msg_type_number_t threadCount;
    task_t task;

    kern_return_t kernReturn = task_for_pid(mach_task_self(), getpid(), &task);
    if (kernReturn != KERN_SUCCESS) {
        return -1;
    }

    kernReturn = task_threads(task, &threadList, &threadCount);
    if (kernReturn != KERN_SUCCESS) {
        return -1;
    }
    vm_deallocate (mach_task_self(), (vm_address_t)threadList, threadCount * sizeof(thread_act_t));

    return threadCount;
}
Emmanuel
  • 2,897
  • 1
  • 14
  • 15
6

This one works on the device as well:

#include <pthread.h>
#include <mach/mach.h>
// ...
thread_act_array_t threads;
mach_msg_type_number_t thread_count = 0;

const task_t    this_task = mach_task_self();
const thread_t  this_thread = mach_thread_self();

// 1. Get a list of all threads (with count):
kern_return_t kr = task_threads(this_task, &threads, &thread_count);

if (kr != KERN_SUCCESS) {
    printf("error getting threads: %s", mach_error_string(kr));
    return NO;
}

mach_port_deallocate(this_task, this_thread);
vm_deallocate(this_task, (vm_address_t)threads, sizeof(thread_t) * thread_count);
Lukasz
  • 19,816
  • 17
  • 83
  • 139
  • What is the purpose of the mach_thread_self() and mach_port_deallocate() calls? They are only used by each other and the type returned by mach_thread_self() does not match the type of the argument it is passed as into mach_port_deallocate(). The code "works" without them. Is something being leaked? Why? Based on what references? https://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_deallocate.html & https://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_thread_self.html are what I read. – gps Jan 23 '22 at 21:22
3

"threadDictionary" is information about a specific NSThread. It's not the total number of threads.

If you want to keep track of "NSThread" objects you've created, you'll probably need to create your own NSMutableArray and add new NSThread objects to it and make certain the objects are valid and correct (i.e. threads are executing, threads are finished or cancelled, etc.) each time you want to do a count of your NSThreads.

This likely still would not give you exactly what you want because NSThread isn't the same thing as threads created and/or references via Grand Central Dispatch (GCD) or other kinds of threads (e.g. pthreads).

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215