-1

In C++, the standard library provides the unsigned int std::thread::hardware_concurrency(); function as a platform-independent way to get the number of available concurrent threads at runtime.

In Golang, the same information is provided by the GOMAXPROCS environment variable which is provided by the runtime.

Does C (including standard and common multithreading libraries, like pthreads) have any such high-level API for getting the number of concurrent threads in a platform-independent way, or is it required to parse system information files to get that kind of information?

Jules
  • 14,200
  • 13
  • 56
  • 101
  • 1
    Look at http://stackoverflow.com/questions/12387436 and http://stackoverflow.com/questions/12387828/what-is-the-maximum-number-of-threads-that-pthread-create-can-create. – RastaJedi Aug 13 '16 at 17:39
  • Are you after the maximum *possible* number of threads ("*get the maximum possible number of threads*") , or the number of currently running threads (for a process? all processes?)? – alk Aug 13 '16 at 17:57
  • @alk I'm trying to figure out the maximum number of threads so that I can adjust how many are spawned accordingly. – Jules Aug 13 '16 at 18:06
  • @RastaJedi one of those is unanswered, and the other isn't really related to what I'm asking about. Thanks for the resources though! – Jules Aug 13 '16 at 18:06
  • One was marked as a dupe of the other, so I decided to include both. I didn't mark yours as a dupe, I just wanted to post it since it's what I found when I searched max number of threads. (The unanswered one I posted because of the `cat /proc/sys/kernel/threads-max`, I thought maybe that was useful to you and also that he's trying to figure out max threads per process, since that might be something you are also interested in). – RastaJedi Aug 13 '16 at 18:47
  • Go has much different threading model and GOMAXPROCS is only a promise, that go does not start more this number of system threads. It is not limit, how much threads can run. In C/C++ is up to you, how much threads you will start. – lofcek Aug 13 '16 at 20:47
  • @lofcek I'm well aware; I'm just using it as an example of this kind of data being exposed to the developer. – Jules Aug 13 '16 at 22:36

2 Answers2

2

Does C provide an API to programatically get the maximum possible number of threads on a system?

No.

... is it required to parse system information files to get that kind of information?

Yes, under Linux at least /proc/sys/kernel/threads-max contains the number you are after.

alk
  • 69,737
  • 10
  • 105
  • 255
-3

Native C programming language does not have a threading model, but you could use a library like OpenMP or pthreads or Linux functions.

OpenMP has:

int omp_get_num_procs();

pthreads has:

int pthread_num_processors(); or int pthread_num_processors_np();

If you are using Linux you can find the number of cores:

$ cat /proc/cpuinfo

You could filter it with grep:

$ grep processor /proc/cpuinfo | wc -l

Or you could use get_nprocs:

#include <sys/sysinfo.h>
int get_nprocs(void);
Franko Leon Tokalić
  • 1,457
  • 3
  • 22
  • 28
  • 6
    "*Native C programming language does not have a threading model ...*" no, C11 has. – alk Aug 13 '16 at 17:55
  • 2
    C11 thread's doc's are here: http://stackoverflow.com/documentation/c/4432/threads-native#t=201608131810495214013 – alk Aug 13 '16 at 18:11
  • 1
    ... and the C11 Standard (draft) on those is here: http://port70.net/~nsz/c/c11/n1570.html#7.26 – alk Aug 13 '16 at 18:18