Thread: passing struct to pthread_create

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    passing struct to pthread_create

    Hi All,

    This compiles and works OK but I want to check I've done it right.

    I have no control over the definition of MYSTRUCT but I do have control over the rest. There is one MYSTRUCT handle for all processes to share and use thread safely.

    I have to pass a copy of the client descritor and the shared MYSTRUCT handle to each connection thread.

    Problem is the server is falling over after about a day.

    Thanks for your help, rotis23


    Code:
    //in /usr/include header file
    typedef void* MYSTRUCT;  
    
    //in local header file
    struct thread_data
    {
            MYSTRUCT mainstruct;
            int client_socket;
    };
    
    
    //in main program
    int serverloop(MYSTRUCT working_struct)
    {
    
    	struct thread_data *myTD = NULL;
    
    	//initiased other stuff
    
    	for(;;)
    	{
    		cli_size = sizeof(cli);
    		client = accept(s,(struct sockaddr*)&cli,(socklen_t*)&cli_size);
    		if(client == -1)
    		{
    			//error handling
    		}
    
    		myTD = (struct thread_data*)malloc(sizeof *myTD);
    		myTD->mainstruct = working_struct;
    		myTD->client_socket = client;
    
    		res = pthread_create(&tid,&thread_attr,HandleRequest,myTD);
    		if (res != 0)
    		{
    			//error handling
    		}
    		pthread_detach(tid);
    	}
    
    	//close stuff
    }
    
    void *HandleRequest(void *argp)
    {
    
            struct thread_data *myTTD;
    
            myTTD = (struct thread_data*)argp;
    
    	//do stuff with (int)myTTD->client_socket and (MYSTRUCT)myTTD->mainstruct
    
    	close((int)myTTD->client_socket);
            free(myTTD);
            pthread_exit(NULL);
            return NULL;
    }

  2. #2
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    at the first sight atleast parts of HandleRequest should be protected using mutex locks,
    it can be disaster if two threads are accessing same function and using some same variables.
    i assume this is not your full code what you'r using in you server daemon...

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I don't see why I should need mutexes.

    Each client (and associated pthread) is given their own copy of the thread_data struct.

    The only thing shared is the MYSTRUCT handle which is used in thread safe library.

    It's all my code apart from the library I'm using that is based around MYSTRUCT.

  4. #4
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    maybe, put 'pthread_detach(tid);' inside else clause to avoid calling it if thread creation fails
    or 'continue;' to '//error handling' in both case.
    Last edited by tjohnsson; 05-03-2004 at 03:13 AM.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    Yes - good idea. I'm not seeing syslogs to that effect - but it's a good idea.

    I wonder if that would cause the server to fail?

    The weird thing is that I'm not getting a seg fault (I'm catching the signal) - the program is failing like it's been sent a SIGKILL signal (kill -9).

    If it's not a memory violation - maybe it's because I'm not controlling the resources properly i.e. I'm using the max num pthread value.

    Hmmmm.

    Any other ideas anyone?

    Thanks for your input tjohnsson!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. passing struct
    By Doxygen in forum C Programming
    Replies: 2
    Last Post: 10-26-2006, 03:05 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Passing a struct
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 03-14-2005, 11:02 AM
  5. passing struct to function help
    By staticalloc in forum C Programming
    Replies: 4
    Last Post: 10-06-2004, 08:30 AM