UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-11-26 08:03 PM

Avel
Guest

Re: Problem with multithread server

Hi, in the programm that follows when the second client try to connect ,blocks until the first client terminates...I suppose that there is problem with the pthread_join...or the while loop...Any help?Thanks:cry:
:confused:

struct arguments {
	   int ns;
	   struct head_tail *s1;
	   struct messageList *s2;
	   int thread_id;
       };


struct arguments arg[NUM_THREADS];        	

void *thread_function( void *arg )
{
	struct arguments *data;
	int n,threadid;
	struct head_tail *k1;
	struct messageList *k2;
	data=(struct arguments *) arg;
	
    n=data->ns;
    threadid=data->thread_id;
	
	k1=data->s1;
	k2=data->s2;
    select_option(ns,s1,s2,threadid);

	pthread_exit(NULL);
}	

while(1)                 
        { 
                printf("Server waiting for connections\n");
                ns=accepting(sd,sockaddr,addrlen);                    //Kaloume ti sunarthsh accepting h opoiaapodexetai gia connect twn clients 
                                                                      //pou briskontai stin oura anamonis
                printf("Accepted...\n");
				bzero(buf,sizeof(buf));
                //getpid();          
				t++; 
                /* Initialize and set thread detached attribute */
                 pthread_attr_init(&attr);
                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
        		
                 printf("Creating thread %d\n", t);
				 
                 rc = pthread_create(&threads[t],&attr, thread_function,(void *)t);
                 if (rc)
                    {
                        printf("ERROR; return code from pthread_create() is %d\n", rc);
                        exit(-1);
                    }
					printf("THREAD[T]:%ld\n",threads[t]);
				tid = pthread_self();

				printf("To id tou nimatos einai:%ld\n",tid);
                /* Free attribute and wait for the other threads */
                 pthread_attr_destroy(&attr);
                 	 rc = pthread_join(threads[t],NULL);
                 if (rc)
                    {
                        printf("ERROR; return code from pthread_join() is %d\n", tid);
                        exit(-1);
                    }
                 printf("Completed join with thread %d status= %ld\n",t, status);
                 
                  
			
		         close(ns);   
      }//end of while

 printf("Main: program completed. Exiting.\n");
		   pthread_exit(NULL);

#2 2008-11-26 09:49 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Problem with multithread server

Yeah, it's the pthread_join()...  That will explicitly wait until the thread exits...  If you
want the parent thread to continue on accepting more clients, you don't want to do
that join...  You probably want to create the thread detached (or have it do pthread_detach()
itself), and have the parent thread just completely ignore the created threads after
that point...  (The threads will need to close the socket themselves in that case, too!)
Or, if you really want to determine when the threads exit for some reason, you can
maybe periodically do pthread_tryjoin_np() if your system has it...

Offline

Board footer

Powered by FluxBB