text: /*
  *  thrdcreat.c - Illustrate creating a thread
  */
  
  #include <stdlib.h>
  #include <stdio.h>
  #include <pthread.h>
  #include <unistd.h>
  
  void task1(int *couter);
  void task2(int *couter);
  void cleanup(int counter1, int counter2);
  
  int g1 = 0;
  int g2 = 0;
  
  int main(int argc, char *argv[])
  {
      pthread_t thrd1, thrd2;
      int ret;
  
      /* Create the first thread */
      ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);
      if (ret) {
          perror("pthread_create: task1");
          exit(EXIT_FAILURE);
      }
  
      /* Create the second thread */
      ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);
      if (ret) {
          perror("pthread_create: task2");
          exit(EXIT_FAILURE);
      }
  
      printf("pthread task  id: %d\n", (int)pthread_self());
      pthread_join(thrd1, NULL);
      pthread_join(thrd2, NULL);
  
      cleanup(g1, g2);
      exit(EXIT_SUCCESS);
  }
  
  void task1(int *counter)
  {
          printf("pthread task1 id: %d\n", (int)pthread_self());
      while (*counter < 5) {
          printf("task1 count: %d\n", *counter);
          (*counter)++;
          sleep(1);
      }
  }
  
  
  void task2(int *counter)
  {
      printf("pthread task2: %d\n", (int)pthread_self());
      while (*counter < 5) {
          printf("task2 count: %d\n", *counter);
          (*counter)++;
          sleep(1);
      }
  }
  
  
  void cleanup(int counter1, int counter2)
  {
      printf("total iterations: %d\n", counter1 + counter2);
  }
  