线程回收

来源:互联网 发布:高淇java 编辑:程序博客网 时间:2024/06/09 23:45

主等子pthread_join ,pthread_exit

how to 回收 5 thread
thread_join can;t because it need 顺序
thread_detach how to retrieve ,pthread_exit(NULL)


#include <pthread.h>#include <stdio.h>#include <stdlib.h>int thread[5]={0};void *closeit(int k);void *m_cbf(void *__args);int main () {pthread_t m_threadId[5];int i;void *p;for (i=0;i<5;i++) {p=&i;// pthread_create(&m_threadId[i],NULL,m_cbf,(void *)i);pthread_create(&m_threadId[i],NULL,m_cbf,p);// printf("main thread m_threadId[%d]=%d\n",i,m_threadId[i]);}// pthread_join(); is block and must fix sequence to reback pthread_create// sleep(10);// for (i=0;i<5;i++) {// pthread_join(m_threadId[i],NULL);// printf("thread %d retrieve\n",i);// }// while (1 ) {// sleep (1);// }pthread_exit(NULL);printf("line61\n");return 0;}void *m_cbf(void *__args) {int k;void *q;q=__args;pthread_detach(pthread_self());// k=(int)__args;k=*(int *)q;// k=(int)*q;thread[k]=1;printf("thread %d\n",k);while (1) {closeit(k);}}void *closeit(int k) {pthread_t tid;tid=pthread_self();printf("close thread %d\n",k);// return  NULL;pthread_exit(NULL) ;// exit ;}


0 0
原创粉丝点击