![](/img/trans.png)
[英]What to use pthread_join,pthread_exit or pthread_cancel while thread in sleep and need to close gracefully?
[英]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連接。
#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中,所有函數都返回3,這意味着線程已被殺死。
pthread_join
不會殺死線程,它會等待線程完成。 如果要殺死線程,請使用pthread_kill
。 但這必須在 pthread_join
之前完成,否則線程已經退出。
pthread_cancel
請求線程在下一個取消點終止,並且可能比使用pthread_kill
更安全。
pthread_exit
退出當前線程。
在pthread_join()
添加
/* Last thing that main() should do */
pthread_exit(NULL);
有關更多詳細信息,請參閱此處 。
pthread_join()用於使main()線程等待,直到所有線程完成其執行。 當特定線程完成執行時,控制權到達pthread_join()。 因此,如果您嘗試取消/殺死該特定線程,將返回錯誤。
示例代碼
#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
#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.)
}
某種情況下會很不錯...但是如果您只是在等待線程(大概只有一個您關心的線程)終止/返回,那么pthread_join就足夠了。
pthread_join()函數等待線程指定的線程終止。 如果該線程已經終止,則pthread_join()立即返回。
http://www.kernel.org/doc/man-pages/online/pages/man3/pthread_join.3.html
檢查手冊頁上的用法/警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.