threads and semaphores

This is a discussion on threads and semaphores within the Linux Programming forums, part of the Platform Specific Boards category; I'm using the following code to demonstarte threads and semaphores. it is works well all the way but the program ...

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    threads and semaphores

    I'm using the following code to demonstarte threads and semaphores. it is works well all the way but the program hangs at the point where the threads join. They never join and program hangs there indefinatly. Anybody please, what am I doing wrong??!!

    Main Code:

    Code:
    void* thread_function(void* arg);
    sem_t bin_sem;
    
    int main() {
     int res;
     void* thread_return;
     pthread_t a_thread;
    
     res = sem_init(&bin_sem, 0, 0);
     if(res != 0) {
            printf("Failed to initialize semaphore. Exiting..\n");
            exit(EXIT_FAILURE);
     }
    
     res = pthread_create(&a_thread, NULL, thread_function, NULL);
     if(res != 0) {
            printf("Failed to create a thread. Exiting..\n");
            exit(EXIT_FAILURE);
     }
    
     int i = 5;
    
     for(i = 5; i > 0; i--){
     printf("This is parent!!\n");
     sem_post(&bin_sem);
     sleep(1);
     }
    
     printf("Waiting for thread to end..\n");
     res = pthread_join(a_thread, &thread_return);
     if(res != 0){
            printf("Failed to join thread. Exiting . . \n");
            exit(EXIT_FAILURE);
     }
    
     printf("Thread joined\n");
     sem_destroy(&bin_sem);
     exit(EXIT_SUCCESS);
    }



    Thread Code:
    Code:
    void* thread_function(void* arg) {
      sem_wait(&bin_sem);
    
      int i;
      for( i=5; i > 0; i--){
            printf("This is child!!\n");
            sleep(1);
            sem_wait(&bin_sem);
      }
      pthread_exit(NULL);
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm not an expert on semaphores, but it seems your thread makes 6 calls to sem_wait, while the parent only calls sem_post 5 times, and so your thread is waiting on a final sem_post that will never come, and hangs, which in turn hangs your thread join.

    If you put a printf() outside of the for() loop in the thread, my guess is you'll never see it. (But you'll see 5 "This is child!!")
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores and threads
    By komalwaseem in forum Linux Programming
    Replies: 6
    Last Post: 04-28-2009, 06:40 AM
  2. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  3. Threads, Linked Lists, Semaphores, Critical Section ...
    By _jr in forum Windows Programming
    Replies: 4
    Last Post: 06-21-2006, 08:14 AM
  4. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 09:55 AM
  5. Protected Threads or Semaphores
    By cloudy in forum C++ Programming
    Replies: 2
    Last Post: 08-16-2005, 07:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21