0

I want to use a bunch of pthreads in my application. To get familiar with the pthread library, I started with a small demo application (see attached sourcecode).

If I create 200 threads, all works well. However, if I increase the number of threads to 2000 the application crashes with a segfault. According to gdb the segfault happens at pthread_join. Unfortunately I could'nt figure out why this is happening.

First, I thought, that my linux machine could'nt handle that many threads, so I increased the value in /proc/sys/kernel/threads-max, but that didn't change anything.

What am I doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define THREADS 200 //If I increase this value to 2000, the application crashes with a segfault


struct tInfo_t{
    int sockfd; 
}; 

pthread_t workerThreads[THREADS];
struct tInfo_t tInfo[THREADS]; 


void *handle(void *arg){ 
    struct tInfo_t threadArgs = *((struct tInfo_t*)arg); 

    pthread_exit(0); //do nothing, just exit 
    return NULL; //to make the compiler happy
}

int main(int argc, char *argv[])
{  
    int i = 0; 

    //create a few threads
    for(i = 0; i < THREADS; i++){
        if(pthread_create(&workerThreads[i], 0, handle, (void*)&tInfo[i]) == -1){
            printf("couldn't create thread. %d \n", workerThreads[i]); 
            return EXIT_FAILURE;    
        }
        printf("Thread #%d spawned\n", i); 
    }

    //wait until all threads finished their job
    for(i = 0; i < THREADS; i++){
        pthread_join(workerThreads[j], NULL); 
    }

    return EXIT_SUCCESS; 
}
user2494129
  • 717
  • 2
  • 13
  • 24
  • 1
    It seems that you just have a typo. You are using `j` instead of `i` for `pthread_join`. – Jens Gustedt Nov 08 '14 at 17:33
  • Thanks for the hint, but the typo comes from copying the source to stackoverflow. Sorry for that! I don't have `j`declared, so the compiler wouldn't let me compile it that way. – user2494129 Nov 08 '14 at 17:43
  • I suspect, due to lack of resources, thread creation failed after a certain point. Check the return value of `pthread_create()`. If there's not enough resources for creating a thread, `pthread_create()` could fail. In that case, you would be trying to join with a non-existent thread. – P.P Nov 08 '14 at 17:46
  • 1
    @BlueMoon he checks the return value of `pthread_create()`. – n. m. could be an AI Nov 08 '14 at 17:48
  • 1
    Copying your code over to the input box should involve only a few mouse gestures and no typing, hence no typos. Please edit your question and paste the actual code you are running. – n. m. could be an AI Nov 08 '14 at 17:50
  • 4
    @n.m. hmm...checking against -1 is not necessarily the enough to catch failure. Try with: `if(pthread_create(&workerThreads[i], 0, handle, (void*)&tInfo[i]) != 0){ .. }` – P.P Nov 08 '14 at 17:53
  • Are you running this on a 32bit machine? – Nick Wilkerson Nov 08 '14 at 17:59
  • Unrelated: if you think 200 threads isn't enough, 2000 threads isn't the solution. Eventually you're going to have to architect a proper thread pool for whatever end-goal you have in mind in managing these threads. Related: Then actual segfault stack trace from a debugger would be helpful, but I suspect you're thread handle being passed to `pthread_join` is in-fact invalid (likely NULL; its initial value) due to collapse in proper detection of a failed `pthread_create` as @BlueMoon's comment suggests. – WhozCraig Nov 08 '14 at 18:05
  • I know, that increasing the number of threads isn't the proper solution. I just wanted to play a little bit with the thread/stack limit on my system to see, what's happening, if i change certain values with the `ulimit`system call. @BlueMoon: Thanks, you are absolutely right, checking against `-1` isn't enough. I changed that, and now the segfault disappeared. Thanks a lot! – user2494129 Nov 08 '14 at 18:16

0 Answers0