微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pthread_join函数杀死执行后的线程或我们需要调用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("%dn",pthread_create(&p1,NULL,&run,(void*)&p1)); printf("%dn",pthread_create(&p2,(void*)&p2)); printf("%dn",pthread_join(p1,NULL)); //usleep(1000); printf("%dn",pthread_join(p2,NULL)); printf("before exitn"); printf("%dn",pthread_cancel(p1)); printf("after exitn"); printf("%dn",pthread_cancel(p2)); printf("both thread exitedn"); printf("%dn",NULL)); printf("%dn",NULL)); printf("terminatedn"); printf("%dn",pthread_kill(p1,0)); printf("%dn",pthread_kill(p2,0)); printf("extn"); printf("%dn",NULL)); printf("jionn"); return 0; } void *run (void *p) { int *i = (int*)p; printf("created i = %dn",*i); }

这是我正在使用的代码在这个pthread_cancel的所有函数返回3表示线程已经被杀死。

具有触摸屏function的Windows应用程序(C ++ / C#)的屏幕键盘

我可以通过.Net获得与安装的打印机相关的图标吗?

与模板类的Windows DLL链接错误

在哪里存储Windows服务的X509证书?

CryptDecrypt在解码string的末尾返回随机字符?

C#Datagridview:获取comboBox列中的选定项目

静态/dynamic链接混淆

什么决定了在调用delete的时候写入C ++指针的内容

如何获得在任务栏中减less的程序的窗口标题

Linux中间隔定时器的精度是多less?

pthread_join不会杀死线程,它会等待线程完成。 如果你想杀死一个线程,然后使用pthread_kill 。 但是这必须在 pthread_join 之前完成,否则线程已经退出

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

pthread_exit退出当前线程。

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

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

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

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

在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 %ldn",t); rc = pthread_create(&thread[t],BusyWork,(void *)t); if (rc) { printf("ERROR; return code from pthread_create() is %dn",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 %dn",rc); exit(-1); } printf("Main: completed join with thread %ld having a status of %ldn",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,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.) }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐