lottoria Kang
lottoria Kang

Reputation: 1

why pThread exit causes main process termination?

I'm testing based on linux 2.6.21 and pThread library. I tried several cases in order to find out workaround how to avoid main process termination. But, I didn't find out it. Please, tell me why exiting of thread function causes main process to be terminated? Here is test code below,

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <sys/msg.h>
    #include <sys/types.h>
    #include <sys/ioctl.h>
    #include <sys/signal.h>
    #include <linux/input.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <dlfcn.h>
    #include <time.h>
    #include <pthread.h>

    int handle = 0;
    int main_loop = 0;

    void *testThread(void *pParm)
    {
      int i;
      for (i=0; i < 5 ; i++){
        printf("====testThread loop %d\n", i);
        sleep(1);
      }

      if (main_loop == 1){
      exit(0);
      }
      else if (main_loop == 2)
      {
        sleep(10);
        exit(0);
      }
      else if (main_loop == 3)
      {
        pthread_exit(NULL);
      }
      else if (main_loop == 4)
      {
        sleep(10);
        pthread_exit(NULL);
      }
    }

    int main(int argc, char *argv[])
    {
      pthread_t pTestThread;

      int i, ret;

      if (argc == 2){
          main_loop = atoi(argv[1]);
      }
      if (argc == 3){
          main_loop = atoi(argv[1]);
          handle = atoi(argv[2]);
      }

      ret = pthread_create(&pTestThread, NULL, (void *)testThread, NULL);
      if (0 == ret){
        if (handle == 0)
          pthread_detach(pTestThread);
        printf("====Thread creation okay!\n");
      }else{
        printf("====Thread creation error!\n");
        return 0;
      }

      if (handle == 1)
      {
        printf("====pthread_join waiting\n");
        pthread_join(pTestThread, (void **)&ret);
        printf("====pthread_join ret %d\n", ret);
      }

      for (i=0; i < 20; i++)
      {
        printf("====Main loop %d\n", i);
        sleep(1);
      }

      printf("====Main Exit\n");
      return 0;
    }

In this code, I have never seen the log of "====Main Exit" with a various combinations (argument 2nd and 3rd).

Upvotes: 0

Views: 1248

Answers (2)

asio_guy
asio_guy

Reputation: 3767

Looking at your code, ( assuming main_loop and handle are global )

 if (main_loop == 1){ // Main thread will exit for if 1st argument is 1
      exit(0);
      }
      else if (main_loop == 2) // Main thread will exit for if 1st argument is 2
      {
        sleep(10);
        exit(0);
      }
      else if (main_loop == 3) // Main thread should not exit if 3
      {
        pthread_exit(NULL);
      }
      else if (main_loop == 4)
      {
       // Main thread should not exit if 3, delay however is 10 seconds
        sleep(10);
        pthread_exit(NULL);
      }
   // Interestingly for all other values main should run as usual

try running your binary as

 ./a.out 3 1 
 ./a.out 4 1 
 ./a.out 123 1

Note - 2nd argument is always 1, for execution to be sequenced.

Upvotes: 0

kaitian521
kaitian521

Reputation: 578

. Please, tell me why exiting of thread function causes main process to be terminated?

Yes, In your thread function, you use "exit()", This function can "Terminates the process normally, performing the regular cleanup for terminating programs."

you can see http://www.cplusplus.com/reference/cstdlib/exit/ for more details:

Calling this function destroys all objects with static duration: A program with multiple threads running shall not call exit (see quick_exit for a similar function that does not affect static objects).

So if you use pthread_exit instead of exit, you can see

====Main loop 18
====Main loop 19
====Main Exit

By the way,

ret = pthread_create(&pTestThread, NULL, (void *)testThread, NULL);

should be

ret = pthread_create(&pTestThread, NULL, testThread, NULL);

Upvotes: 1

Related Questions