Thread: regarding thread management in C

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    regarding thread management in C

    Hi,

    I am a beginner with threads in C. I need help regarding thread management in C.

    my GNU thread library version is the following:
    NPTL 2.9

    My main program creates a thread. That thread must complete certain tasks before the program terminates. But what happens in most of the cases is that the main program ends without the thread finishing its job.

    I write part of the code here.

    Thanks

    Code:
    // main program
    
    #include "server.h"
    
    
    int main(int argc, char* argv[])
    {
       
       // Initialize the server i..e create the server thread
      serverInitialize(1000);
      
      // Task 1: in the main 
      // Task 2:  in the main
    
      // Now wake the server
       Wake();
    
      /**
        comment: the above call wakes the server i.e. 
        the thread that was created in the call to serverInitialize(), 
        and this must run the loop in the serverFunction(), 
         but it does not and is terminated most of the time 
      */
    
    
       return 0;
    }
    
    
    // server.c
    
    static rtl_pthread_t server_thread;
    
    /** @brief Semaphore descriptor */
    static rtl_sem_t wake_semaphore;
    
    /* Prototypes */
    static void serverFunction(void *t);
    
    signed char serverInitialize( unsigned long value ){
    
        int ret;
        
        rtl_sem_init(&wake_semaphore, 1, 0);	
    
        ret = rtl_pthread_create( &server_thread, NULL, serverFunction, NULL );
        if (ret){
    		ERROR_MSG("Init Fatal Error: pthread_create returned %d", ret);
    		return -1;
    	}
     
        PRINT_MSG("Initialization OK...");
    }
    
    void Wake(void)
    {
    	
    	/* Activate server */
    	
            printf("\n Server to be waked now\n");
    	rtl_sem_post(&wake_semaphore);
    	return ;
    }
    
    void *serverFunction(void *t){
    
           while (1){
    		/* Wait for next invocation */
    		rtl_sem_wait(&wake_semaphore);
                    
                   // Perform task 1
                   // Perform task 2
                   // Perform task 3
                   ....
                   // Perform last task
                   
            }
    }
    Looking forward to the comments and input

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    While it doesn't answer your primary question, I'm seeing two different serverFunction()s. The first is the prototype,
    Code:
    static void serverFunction(void *t);
    and the second is the definition:
    Code:
    void *serverFunction(void *t){...
    Kevin

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So implement in server.c a
    serverFinalize()
    function which waits for server_thread to exit (say rtl_pthread_join).
    Call this from main() before you want to exit, and it will wait just long enough for your thread to exit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Quote Originally Posted by Salem View Post
    So implement in server.c a
    serverFinalize()
    function which waits for server_thread to exit (say rtl_pthread_join).
    Call this from main() before you want to exit, and it will wait just long enough for your thread to exit.
    Hi, thanks for the reply.

    Infact, I did try that, but somehow similar behavior repeats. My main program still does not wait long enough.

    Does it have to do with how the thread is created initially


    thanks,

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-09-2010, 07:31 PM
  2. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  3. Message box as thread to stop main thread
    By Yasir_Malik in forum Windows Programming
    Replies: 8
    Last Post: 04-11-2006, 11:07 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM

Tags for this Thread