为什么pThread退出导致主进程终止?

时间:2015-09-15 02:26:22

标签: c multithreading pthreads

我基于linux 2.6.21和pThread库进行测试。 我尝试了几种情况,以找出解决方法如何避免主进程终止。但是,我没有发现它。 请告诉我为什么退出线程函数会导致主进程被终止? 这是下面的测试代码,

    #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;
    }

在这段代码中,我从未见过&#34; ====主要退出&#34;的日志。有各种组合(参数第2和第3)。

2 个答案:

答案 0 :(得分:1)

  

。请告诉我为什么退出线程函数会导致主进程   被终止?

是的,在您的主题功能中,您使用&#34; exit()&#34;,此功能可以&#34; Terminates the process normally, performing the regular cleanup for terminating programs。&#34;

您可以查看http://www.cplusplus.com/reference/cstdlib/exit/了解更多详情:

  

调用此函数会破坏具有静态持续时间的所有对象:A   运行多个线程的程序不应调用exit(参见   quick_exit用于不影响静态的类似函数   对象)。

因此,如果您使用pthread_exit而不是退出,则可以看到

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

顺便说一下,

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

应该是

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

答案 1 :(得分:0)

查看您的代码,(假设main_loop和句柄是全局的)

 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

尝试运行二进制文件

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

注 - 第二个参数始终为1,表示执行顺序。