非分离线程使用join函数例子

//非分离线程使用join函数例子:

#include<stdlib.h>

#include<pthread.h>

#include<stdio.h>

#include<sched.h>

#include<errno.h>

void*consumer(void*p)

{

    static a=0 ;

    a++;

    printf("<<<<<<<<<<<<<<<<<<<<<(%u),%d\n",(unsigned)pthread_self(),a);

    pthread_exit(NULL);

}

//如果主函数加上pthread_join(t1, NULL);才可以一直创建线程,如下,

 int main(int argc, char *argv[])  

  { 

          pthread_t  t1, t2, t3; 

        int ret; 

        do{ 

                ret = pthread_create(&t1, NULL, consumer, NULL); 

                 if(ret != 0) 

                 {       

                        printf("create failed,%d\n", ret); 

                         exit(1); 

                } pthread_join(t1, NULL); 

                 

        }while(1); 

        sleep(1); 

     return 0; 

}

 

你可能感兴趣的:(JOIN)