首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pthread_join函数在执行后终止线程,还是我们需要调用p线程_cancel/p线程_exit?

pthread_join函数在执行后终止线程,还是我们需要调用p线程_cancel/p线程_exit?
EN

Stack Overflow用户
提问于 2012-09-05 11:14:16
回答 3查看 9.3K关注 0票数 2

pthread_join()函数在执行后终止线程,或者我们需要调用pthread_cancel()/pthread_exit()

我调用pthread_cancel()/pthread_kill(),它将返回3(即没有附加thread_id的线程)。

代码语言:javascript
复制
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>

void * run (void *);

int main() {
pthread_t p1, p2;
int a = 9;
printf("%d\n", pthread_create(&p1, NULL, &run, (void*)&p1));
printf("%d\n", pthread_create(&p2, NULL, &run, (void*)&p2));

printf("%d\n", pthread_join(p1, NULL));
//usleep(1000);
printf("%d\n", pthread_join(p2, NULL));

printf("before exit\n");
printf("%d\n", pthread_cancel(p1));
printf("after exit\n");
printf("%d\n", pthread_cancel(p2));
printf("both thread exited\n");

printf("%d\n", pthread_join(p1, NULL));
printf("%d\n", pthread_join(p2, NULL));
printf("terminated\n");

printf("%d\n", pthread_kill(p1, 0));
printf("%d\n", pthread_kill(p2, 0));
printf("ext\n");

printf("%d\n", pthread_join(p1, NULL));
printf("%d\n", pthread_join(p2, NULL));
printf("jion\n");

return 0;
}

void *run (void *p) {

int *i = (int*)p;
printf("created i = %d\n", *i);
}

这是我正在使用的代码。在这个pthread_cancel上,wards所有函数返回3,这意味着线程已经被杀死了。

EN

回答 3

Stack Overflow用户

发布于 2012-09-05 11:16:39

pthread_join不杀死线程,而是等待线程完成。如果您想要杀死一个线程,那么使用pthread_kill。但这必须在pthread_join之前完成,否则线程已经退出。

pthread_cancel请求线程在下一个取消点进行迭代,并且可能比使用pthread_kill更安全。

pthread_exit退出当前线程。

票数 7
EN

Stack Overflow用户

发布于 2012-09-05 11:17:45

pthread_join()后添加

代码语言:javascript
复制
/* Last thing that main() should do */
   pthread_exit(NULL);

有关更多细节,请参阅这里

pthread_join()用于使主()线程等待所有线程完成它的执行。控件到达pthread_join(),当该特定线程完成其执行时。因此,如果您试图取消/终止该特定线程,则将返回错误。

示例代码

代码语言:javascript
复制
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define NUM_THREADS 4

    void *BusyWork(void *t)
    {
       int i;
       long tid;
       tid = (long)t;
       printf("Thread %ld starting...\n",tid);
       pthread_exit((void*) t);                 //Exits that current thread.
    }

    int main (int argc, char *argv[])
    {
       pthread_t thread[NUM_THREADS];
       int rc;
       long t;
       void *status;

       for(t=0; t<NUM_THREADS; t++) {
          printf("Main: creating thread %ld\n", t);
          rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t); 
          if (rc) {
             printf("ERROR; return code from pthread_create() is %d\n", rc);
             exit(-1);
             }
          }

      /* Wait for completion */
       for(t=0; t<NUM_THREADS; t++) {
          rc = pthread_join(thread[t], &status);
          if (rc) {
             printf("ERROR; return code from pthread_join() is %d\n", rc);
             exit(-1);
             }
printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
          }

    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL); // Exits main thread.
    }

@Joachim Pileborg

代码语言:javascript
复制
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 4

void *BusyWork(void *t)
{
   int i;
   long tid;
   tid = (long)t;

   printf("Thread %ld starting...\n",tid);

   for(i = 0; i <10000000; i++)
        printf("\n Thread: %ld,  %d", tid, i);

   pthread_exit((void*) t);                 //Exits that current thread.
}

int main (int argc, char *argv[])
{
   pthread_t thread[NUM_THREADS];
   int rc;
   long t;
   void *status;

   for(t=0; t<NUM_THREADS; t++) {
      printf("Main: creating thread %ld\n", t);
      rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t);
      if (rc) {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
         }
      }

//printf("Main: program completed. Exiting.\n");
pthread_exit(NULL); // Exits main thread(work will be done).
//return 0;//( Threads will exit once main exits.)
}
票数 0
EN

Stack Overflow用户

发布于 2012-09-05 11:18:50

一些背景会很好..。但是,如果您只是在等待一个线程(大概只有一个您关心的线程)来终止/返回,那么pthread_join就足够了。

pthread_join()函数等待线程指定的线程终止。如果该线程已经终止,那么pthread_join()将立即返回。

join.3.html

检查手册页的用法/警告。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12280216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档