• Uncategorized

About c : How-do-I-count-the-number-of-running-threads-pthreads

Question Detail

If I search for counting the number of threads an application has, all the answers involve external programs like top. I want to count the threads within the application itself.

I can’t add code at the point of thread creation because it happens inside an immutable library.

I can’t read /proc.

It’s a C/pthreads program running on a few different Unices.

Question Answer

If you can’t read /proc you are a bit in trouble, unless your program communicate with another program which reads /proc

If you don’t want to read /proc because of portability concerns, you might use a library which abstracts that a bit, like libproc does

You could write a tiny wrapper for pthread_create that counts created threads and link against that wrapper after you linked against the immutable library.

Use top -H. But chances are, if you can’t read proc, top won’t work anyway. If thats the case, there is no easy way and it would depend on your specific system.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.