title: Thread Termination issue (C): Last thread not terminating
body: <p>Why is my last thread not terminating?</p>
  
  <p>I want to start all the threads at the same time, so I have ten threads that once created are put to sleep. Once all the threads are created, I wake all of them to start processing the data. The threads complete except the last thread, thread #9. Does anyone know why the last thread isn't terminating and joining all the other pthreads?</p>
  
  <p>Output:</p>
  
  <pre><code>------------------Theatre Seating----------------
  
  0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 
  0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 
  0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 
  0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 
  0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 
  0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 
  0060 0061 0062 0063 0064 0065 0066 0067 0068 0069 
  0070 0071 0072 0073 0074 0075 0076 0077 0078 0079 
  0080 0081 0082 0083 0084 0085 0086 0087 0088 0089 
  0090 0091 0092 0093 0094 0095 0096 0097 0098 0099 
  
  ------------------End of Seating----------------
  
  Enter N value of Customers: 2
  Number of Customers: 2
  thread #0: seller_type1-&gt;st = H
  thread #1: seller_type1-&gt;st = M
  thread #2: seller_type1-&gt;st = M
  thread #3: seller_type1-&gt;st = M
  thread #4: seller_type1-&gt;st = L
  thread #5: seller_type1-&gt;st = L
  thread #6: seller_type1-&gt;st = L
  thread #7: seller_type1-&gt;st = L
  thread #8: seller_type1-&gt;st = L
  thread #9: seller_type1-&gt;st = L
  Seller type: H
  thread #0: Work done
  Seller type: M
  Seller type: M
  Seller name: M1
  
  Seller type: M
  Seller name: M2
  
  Seller type: L
  Seller type: L
  thread #1: Work done
  Seller type: L
  Seller name: M3
  
  Seller type: L
  Seller type: L
  thread #2: Work done
  thread #4: Work done
  thread #5: Work done
  thread #6: Work done
  thread #3: Work done
  thread #7: Work done
  thread #8: Work done
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code> * Created on July 4, 2016, 11:27 AM
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  // seller thread to serve one time slice (1 minute)
  
  #include &lt;stdlib.h&gt;
  
  //Global variables
  //Number of Customers
  int N;
  char * theatre[][10] = { 
          {"0000", "0001", "0002", "0003", "0004", "0005", "0006", "0007", "0008", "0009"},
          {"0010", "0011", "0012", "0013", "0014", "0015", "0016", "0017", "0018", "0019"},
          {"0020", "0021", "0022", "0023", "0024", "0025", "0026", "0027", "0028", "0029"},
          {"0030", "0031", "0032", "0033", "0034", "0035", "0036", "0037", "0038", "0039"},
          {"0040", "0041", "0042", "0043", "0044", "0045", "0046", "0047", "0048", "0049"},
          {"0050", "0051", "0052", "0053", "0054", "0055", "0056", "0057", "0058", "0059"},
          {"0060", "0061", "0062", "0063", "0064", "0065", "0066", "0067", "0068", "0069"},
          {"0070", "0071", "0072", "0073", "0074", "0075", "0076", "0077", "0078", "0079"},
          {"0080", "0081", "0082", "0083", "0084", "0085", "0086", "0087", "0088", "0089"},
          {"0090", "0091", "0092", "0093", "0094", "0095", "0096", "0097", "0098", "0099"}
  };
  
  
  struct seller_type {
    char st;
    int tid;
    int numid;
    char * name;
  };
  
  
  void fillSeat()
  {
      char *c1 = theatre[0][2];
      char *d1 = theatre[0][0];
  
        //asprintf(&amp;d1, "%c%c%c%d\n",c1[0],c1[1],seller_type1-&gt;st,seller_type1-&gt;tid);
        printf("D : %s", d1);
  }
  
  void *sell(void *arg)
  {
    struct seller_type *seller_type1 = arg;
    int i;
    i = 0;
    printf("thread #%d: seller_type1-&gt;st = %c\n", seller_type1-&gt;tid,
       seller_type1-&gt;st);
  
  
    // do work
    while (i == 0) {
    pthread_mutex_lock(&amp;mutex);
    pthread_cond_wait(&amp;cond, &amp;mutex);
    pthread_mutex_unlock(&amp;mutex);
  
      switch (seller_type1-&gt;st) {
  
      case 'H':
        printf("Seller type: H\n");
        //printf("Seller name: %s\n", seller_type1-&gt;name);
  
        //Begin Selling tickets
        i = 1;
        //set time;
  
        break;
  
      case 'M':
        printf("Seller type: M\n");
        printf("Seller name: %s\n", seller_type1-&gt;name);
  
        //Begin Selling tickets
        i = 1;
        //set time;
        break;
  
      case 'L':
        printf("Seller type: L\n");
        //printf("Seller name: %s\n", seller_type1-&gt;name);
  
        //Begin Selling tickets
        i = 1;
        //set time;
        break;
  
      }
  
  
    }
    printf("thread #%d: Work done\n", seller_type1-&gt;tid);
    return NULL;
  }
  
  void displayTheatre()
  {
      int i,j;
      printf("\n------------------Theatre Seating----------------\n\n");
      for(i = 0;i&lt;10;i++)
      {
          for(j = 0;j&lt;10;j++){
              printf("%s ",theatre[i][j]);
          }
          printf("\n");
      }
       printf("\n------------------End of Seating----------------\n\n");
  }
  
  void wakeup_all_seller_threads()
  {
   pthread_mutex_lock(&amp;mutex);
   pthread_cond_broadcast(&amp;cond);
   pthread_mutex_unlock(&amp;mutex);
  
  }
  
  int main()
  {
    N = 0;
    int i = 0;
    struct seller_type *seller_type1;
    displayTheatre();
    pthread_t tids[10];
    seller_type1 = calloc(10, sizeof(struct seller_type));
  
     printf("Enter N value of Customers: ");
     scanf("%d", &amp;N);
     printf("Number of Customers: %d\n", N);
  
    // All error handling ommitted! Yes, ALL!
  
    seller_type1[0].st = 'H';
    seller_type1[0].tid = 0;
    seller_type1[0].numid = 0;
    asprintf(&amp;seller_type1[0].name, "%c%d\n",seller_type1[0].st,seller_type1[0].numid);
    pthread_create(&amp;tids[0], NULL, sell, &amp;seller_type1[0]);
  
    for (i = 1; i &lt; 4; i++) {
      seller_type1[i].st = 'M';
      seller_type1[i].tid = i;
      seller_type1[i].numid = i;
      asprintf(&amp;seller_type1[i].name, "%c%d\n",seller_type1[i].st,seller_type1[i].numid);
      pthread_create(&amp;tids[i], NULL, sell, &amp;seller_type1[i]);
    }
  
    for (i = 4; i &lt; 10; i++) {
      seller_type1[i].st = 'L';
      seller_type1[i].tid = i;
      seller_type1[i].numid = i-3;
      asprintf(&amp;seller_type1[i].name, "%c%d\n",seller_type1[i].st,seller_type1[i].numid);
      pthread_create(&amp;tids[i], NULL, sell, &amp;seller_type1[i]);
    }
  
    wakeup_all_seller_threads();
    //puts("All threads Start");
  
    // wait for all seller threads to exit
    for (i = 0; i &lt; 10; i++) {
      pthread_join(tids[i], NULL);
      //printf("Thread %d joined\n", i);
    }
    //puts("All threads joined");
  
    exit(EXIT_SUCCESS);
  }
  </code></pre>