Results 1 to 5 of 5
Hello,
I am having trouble creating threads. The question is not about the number of concurrent threads, but rather the overall number of threads that I can create during the ...
- 11-04-2009 #1Just Joined!
- Join Date
- Nov 2009
- Posts
- 2
pthread limit question
Hello,
I am having trouble creating threads. The question is not about the number of concurrent threads, but rather the overall number of threads that I can create during the lifetime of my application. I would think that the number of (non-concurrent) threads would be unlimited.
For example, the call to pthread_create fails with error 12 (out of memory) after about 300 threads are created, even though each one finishes very shortly after being created.
Because these threads have very short lifespans, so the number of threads that are actually "alive" at any time is small. Yet pthread_create still fails... any idea why?
Code:#include "pthread.h" #include <iostream> using namespace std; int threadCount = 0; void* threadProc (void*) { cout << "Executing thread " << threadCount++ << endl; pthread_exit (NULL); } int main(int argc, char *argv[]) { while (true) { pthread_t id; int error = pthread_create (&id, NULL, threadProc, NULL); cout << "pthread_create error was " << error << endl; usleep (20000); } sleep (10000); }
- 11-04-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,961
Try calling pthread_join from main() to wait for the actual termination of each thread before continuing the loop. Use that instead of usleep() so you know that the thread has been properly terminated. Also, your value for usleep is only 20 milliseconds, which may mean that you end up with more active threads concurrently than you think.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-04-2009 #3Just Joined!
- Join Date
- Nov 2009
- Posts
- 2
Hi Rubberman,
Thanks for this -- increasing the 20 milliseconds does not solve the problem, but the problem can be resolved by either joining the threads (not possible in the structure of my application), or by creating the thread in a detached state (or calling pthread_detach on it).
Thank you for your help -- hopefully this will help someone else out, too!
- 11-04-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,961
Actually, I think you do need to join or detach a thread in order to properly terminate it. Yes, pthread_exit() will stop the thread, but I'm not sure that will clean up all the handles and other resources it has used up. From my reading of the man pages, the proper function to use is pthread_detach(). Here is the description from the man page:
So, in your example, you need to do this:Code:DESCRIPTION The pthread_detach() function shall indicate to the implementation that stor- age for the thread thread can be reclaimed when that thread terminates. If thread has not terminated, pthread_detach() shall not cause it to terminate. The effect of multiple pthread_detach() calls on the same target thread is unspecified.
Code:while (true) { pthread_t id; int error = pthread_create (&id, NULL, threadProc, NULL); if (!error) { pthread_detach(id); } else { cout << "pthread_create error was " << error << endl; } usleep (20000); }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-04-2009 #5Just Joined!
- Join Date
- Oct 2009
- Posts
- 7
Yes, at some point pthread_join or pthread_detach should be called on any thread you spawn in this manner. If you do not the resources will not be deallocated correctly for that thread.


Reply With Quote
