hello
I have a client-server program in which I need to accept connections of multiple clients and create a thread to serve each one of them.
I don't know from before the number of the threads that have to be created so for every connection I have to allocate space.
Here's what I do
Code:
pthread_t *tids;
thread_counter=0;
if (( tids = new pthread_t (sizeof ( pthread_t )) ) == NULL )
exit(1);
if(thread_counter!=0){
tids+1=new pthread_t (sizeof ( pthread_t ));
tids=tids+1;
if(tids==NULL)
cout<<"error with realloc"<<endl;*/
}
if(err=pthread_create(tids,NULL,parse,(void*)argument))
{
perror("pthread create\n");
exit(1);
}
thread_counter++;
The above is in a while loop and allocates space for the first thread and then creates it.
For every new connection it allocates space for a new thread(thread_counter>0) and creates it.
The problem seems to appear with the pthread_create for the second thread etc.
(The program is compiled with g++ and that's why new is used,but vectors are not allowed so the rest of the program is in C)