How many threads is too many?

So, by now, we know how to create an application process with a few threads executing within it. We will repeat a code snippet from our very first demo program, ch14/pthreads1.c, as follows:

#include <pthread.h>#define NTHREADS 3[...]int main(void){  [...]  for (i = 0; i < NTHREADS; i++) {        ret = pthread_create(&tid, NULL, worker, (void *)i);        if (ret)              FATAL("pthread_create() failed! [%d]\n", ret);  }[...]

Clearly, the process—well, we really mean the main thread of the process (or application)—goes in a loop, and each loop iteration creates a thread. So, when it's done, we will have three threads in addition to the main thread, which is a total of four threads, alive in the process.

This is obvious. The point here ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.