Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I have a problem that after some time pthreads is no longer able to create new threads. The funny thing is, I do not create too much threads at ...
  1. #1
    Just Joined!
    Join Date
    May 2011
    Posts
    3

    pthreads is running out of threads

    Hi,

    I have a problem that after some time pthreads is no longer able to create new threads. The funny thing is, I do not create too much threads at the same time but threads out of threads. What I do is the following:

    1. create a thread
    2. within this thread I wait some milliseconds
    3. now I do some work which takes some time
    4. out of this thread I start with 1.
    5. as soon as the new thread was created successfully I clean up the current thread and exit it

    So as far as I understand it the previous thread should have been gone long time before I could run out of threads because the total number of threads that runs at the same time is exceeded. Unfortunately this is not what I notice: after about 40..50 thread creations pthread_create() fails with EAGAIN.

    So what could be the reason for that? Thread-creation is done quite simple:

    Code:
       pthread_t *thread;
       int        ret;
    
       thread=(pthread_t*)malloc(sizeof(pthread_t));
       if (!thread) return NULL;
    
       ret=pthread_create(thread,NULL,start_routine,arg);

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Do you call pthread_exit()? Does the creating thread call pthread_join()? If you don't do this, the resources are still used up. The pthread_exit() call will terminate the thread, and pthread_join() called by the creating, or other thread, will merge their resources so that you won't run into this situation.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Apr 2011
    Posts
    19

    Need to detach

    I think if you call pthread_detach it will fix your problem. Without it, or setting the detach state attribute using pthread_attr_init, the resource will wait for a pthread_join.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...