niklas-komani
January 12th, 2007, 04:58 PM
Hey, guys I encountered a strange problem concerning Thread creation.
I'm trying to write a Server where every Client is served by a thread, which to me seems prity effective in my case since I'm using blocking recvs and most of the Threads will stay blocked for most of the time. Now I encountered that after about a few hundred connects of which each starts a thread which in my test cas closes after less then a second due to client disconnect, the Server can't launch new ClientThreads while the accept call still returns valid sockets (It's after 255 attempts on my Debian Sarge Server and at about 380-382 attempts on Ubuntu and Debian Etch). Another crazy problem is that taking pauses of a few secconds between the connection attempts doesn't make any difference. Which excludes race Conditions as the last Thread will be closed when a new one is opned.
Luckily I was able to shrink the problem down to a very small piece of Code:
#include <iostream>
#include <pthread.h>
void* mythread(void* ptr){
std::cout<<"Thread started"<<std::endl;
return 0;
}
int main (int argc, char* argv){
for(int i=0;i<500;i++){
pthread_t MyThread;
pthread_create(&MyThread, 0, &mythread, 0);
//sleep(1); Commenting this in doesn't solve the problem
}
return 0;
}
Note: I left out a Mutex for std since the Output is not scattered and this would make the code less simple.(On Mac OS X however std doesn't seem to serliz the Output, but with the sleep commented in this is a not a problem there either)
You can compile the code with "g++ file.cpp -o thread -lpthread"
Then you can run it with "./thread > out.txt" and count the lines of output which should according to the loop be 500 however "grep -c "Thread started" out.txt" only Counts 255 hits on debian sarge and 382 on Etch and Ubuntu.
Can someone please tell me why a process can't open a thread as many times as needed?
Any help will be appreciated!
Cincerly Niklas
I'm trying to write a Server where every Client is served by a thread, which to me seems prity effective in my case since I'm using blocking recvs and most of the Threads will stay blocked for most of the time. Now I encountered that after about a few hundred connects of which each starts a thread which in my test cas closes after less then a second due to client disconnect, the Server can't launch new ClientThreads while the accept call still returns valid sockets (It's after 255 attempts on my Debian Sarge Server and at about 380-382 attempts on Ubuntu and Debian Etch). Another crazy problem is that taking pauses of a few secconds between the connection attempts doesn't make any difference. Which excludes race Conditions as the last Thread will be closed when a new one is opned.
Luckily I was able to shrink the problem down to a very small piece of Code:
#include <iostream>
#include <pthread.h>
void* mythread(void* ptr){
std::cout<<"Thread started"<<std::endl;
return 0;
}
int main (int argc, char* argv){
for(int i=0;i<500;i++){
pthread_t MyThread;
pthread_create(&MyThread, 0, &mythread, 0);
//sleep(1); Commenting this in doesn't solve the problem
}
return 0;
}
Note: I left out a Mutex for std since the Output is not scattered and this would make the code less simple.(On Mac OS X however std doesn't seem to serliz the Output, but with the sleep commented in this is a not a problem there either)
You can compile the code with "g++ file.cpp -o thread -lpthread"
Then you can run it with "./thread > out.txt" and count the lines of output which should according to the loop be 500 however "grep -c "Thread started" out.txt" only Counts 255 hits on debian sarge and 382 on Etch and Ubuntu.
Can someone please tell me why a process can't open a thread as many times as needed?
Any help will be appreciated!
Cincerly Niklas