[SOLVED] how to find number of threads in the process in C Language?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The link in your post refers not to Linux, but to HP-UX, which is the Hewlett-Packard variant of Unix. You can't use that solution in Linux.
The best solution, in my opinion, is to put a wrapper function around pthread_create(). Write this wrapper function so that, in addition to calling pthread_create(), it bumps a thread counter. Of course, your main program must initialize that counter to zero at the beginning. Of course, you need to protect that counter with a mutex.
Make sure that you also create appropriate wrapper functions for decrementing that count.
Once you've done this, you can refer to that count any time you want (with mutex protection, of course). Furthermore, you can place in your pthread_create() wrapper function some additional code to keep an eye on the thread count and report every time that count reaches a multiple of 100, say, or do something appropriate if that count gets too high.
There may be a simpler way to find the thread count, but I'm not aware of it.
The best solution, in my opinion, is to put a wrapper function around pthread_create().
Dear wje_lq, thanks for your response.
Yes. This is a feasible solution. I can adopt this. If I adopt this, I will be in need of another information. For a given thread id, I should be able to decide whether that thread is still existing or terminated.
This situation in my requirement is like this. For the sake of optimizing the time consumption, I will create a new thread for sub functions that can be executed in parallel. After completing the execution of that sub function, that thread has to die or I should assign another sub function to that. At the same time, I must ensure that there are only a limited (maximum) number of threads existing. The process should not contain number of threads more than this threshold value.
All it takes, then, is a simple bookkeeping operation. You could do something like the following example.
Quote:
Originally Posted by chakka.lokesh
No information will be passed.
In the proposed example, this would change.
Quote:
Originally Posted by chakka.lokesh
number of threads depends on the number of cores available in CPU
With such a small number, which would remain constant during any given run of your program, this makes things very simple.
In the proposed example, the following steps would be performed once, at program initialization.
Determine how many cores are available in the CPU. Since this will be done only once per run of the program, you can rely on a crude but effective hack. There are many variants. Here's one:
Code:
#include <stdlib.h>
/* ... */
if(system("grep ^processor /proc/cpuinfo | wc -l > cpucount.txt"))
{
/* insert error handling code here */
}
/* then read the answer from the file cpucount.txt */
Define a struct like the following:
Code:
typedef struct
{
/* tt_status can have one of the following values:
* 0: no thread has been started
* 1: a thread has been started but not finished
* 2: a thread has been finished
*/
int tt_status;
pthread_t tt_thread_id;
} thread_tracker_t,
*thread_tracker_p;
malloc() an array of this struct. The number of elements in the array should be equal to the number you got in step I. Let's call this array thread_status. Zero out each tt_status, or all bytes of thread_status if that's your style.
In the proposed example, the following steps would be taken each time you want to fire off a thread.
Loop through thread_status. For each element where tt_status is 2, set tt_status to 0 and call pthread_join; its first parameter should be the corresponding value of tt_thread_id.
Loop through thread_status again. If no member contains tt_status being 0, you know that you shouldn't fire off a new thread at this point. Otherwise, set xxx to the index in the array of one of the elements containing tt_status equal to 0, and continue with the following step.
Set thread_status[xxx].tt_status to 1. Then fire off a new thread with pt_create(). Let the pointer passed as the first parameter to pthread_create() be &(thread_status[xxx].tt_thread_id). Let the fourth parameter to pt_create() be thread_status+xxx.
Each thread, when started, gets a pointer to the relevant member of thread_status. When that thread is done, it should set the tt_status field to 2 just before calling pthread_exit().
[*]Determine how many cores are available in the CPU. Since this will be done only once per run of the program, you can rely on a crude but effective hack. There are many variants. Here's one:
Code:
#include <stdlib.h>
/* ... */
if(system("grep ^processor /proc/cpuinfo | wc -l > cpucount.txt"))
{
/* insert error handling code here */
}
/* then read the answer from the file cpucount.txt */
Somewhat offtopic, but here's another way to determine the number of cpus/cores:
I referred the man unistd.h.
In that, _SC_NPROCESSORS_ONLN is not there...!!!
One wouldn't expect _SC_NPROCESSORS_ONLN to be defined in unistd.h. unistd.h includes other files. In modern Linux systems, you'll see _SC_NPROCESSORS_ONLN defined in bits/confname.h
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.