关于C#:pthread_join函数在执行后杀死线程,还是我们需要调用pthread_cancel / pthread_exit?

pthread_join function kill the thread after execution or we need to call pthread_cancel/pthread_exit?

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

我正在调用pthread_cancel() / pthread_kill(),该函数返回3,即没有附加带有thread_id的线程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#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\
"
, pthread_create(&p1, NULL, &run, (void*)&p1));
printf("%d\
"
, pthread_create(&p2, NULL, &run, (void*)&p2));

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

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

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

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

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

return 0;
}

void *run (void *p) {

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

这是我正在使用的代码。在此pthread_cancel区域中,所有函数返回3,这意味着线程已被杀死。


pthread_join不会杀死线程,它等待线程完成。如果要杀死线程,请使用pthread_kill。但这必须在pthread_join之前完成,否则线程已经退出。

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

pthread_exit退出当前线程。


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

The pthread_join() function waits for the thread specified by thread
to terminate. If that thread has already terminated, then pthread_join() returns
immediately.

http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_join.3.html

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


pthread_join()之后添加

1
2
/* Last thing that main() should do */
   pthread_exit(NULL);

有关更多详细信息,请参见此处。

pthread_join()用于使main()线程等待,直到所有线程完成其执行。当特定线程完成它的执行时,控制权到达pthread_join()。
因此,如果您尝试取消/杀死该特定线程,将返回错误。

样品代号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    #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...\
"
,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\
"
, t);
          rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t);
          if (rc) {
             printf("ERROR; return code from pthread_create() is %d\
"
, 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\
"
, rc);
             exit(-1);
             }
printf("Main: completed join with thread %ld having a status of %ld\
"
,t,(long)status);
          }

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

对于@Joachim Pileborg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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...\
"
,tid);

   for(i = 0; i <10000000; i++)
        printf("\
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\
"
, t);
      rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t);
      if (rc) {
         printf("ERROR; return code from pthread_create() is %d\
"
, rc);
         exit(-1);
         }
      }

//printf("Main: program completed. Exiting.\
");

pthread_exit(NULL); // Exits main thread(work will be done).
//return 0;//( Threads will exit once main exits.)
}