How to make main thread wait for all child threads finish?

后端 未结 4 1012
梦毁少年i
梦毁少年i 2020-12-01 17:56

I intend to fire 2 threads in the main thread, and the main thread should wait till all the 2 child threads finish, this is how I do it.

void *routine(void *         


        
相关标签:
4条回答
  • 2020-12-01 18:11
    #include<stdio.h>
    #include<pthread.h>
    
    int icnt = 0;   //in non_bss data segment
    pthread_mutex_t lock;     //lock variable created stored into bss data segment
    
    
    void *Thread_count(void* args)      //syncronization 
    {
    pthread_mutex_lock(&lock);              //lock aquire 
        
    icnt++;
    
    for(int x = 1; x <= icnt; x++)
    {
        printf("Hello from Thread_count : %d \n",icnt);
    }
    printf("\n");
    
    pthread_mutex_unlock(&lock);            //release lock  
    pthread_exit(NULL);                     //exit from child thread
    }
    
    
    int main()
    {
    
    pthread_t threads[4];  //created array of {unsigned long int}
    int status = 0;
    
    //creating threads in loop      
    for(int i = 1; i <= sizeof(threads)/sizeof(threads[0]); i++)
    {
        pthread_create(&threads[i], NULL, &Thread_count, NULL);
    }
    
    //waiting for threads in loop
    for(int j = 1; j <= sizeof(threads)/sizeof(threads[0]); j++)
    {
        pthread_join(threads[j], &status);
        
        printf("Thread number : %d     <-->  Thread status : %d\n",j, status);
    }
    
    
    pthread_exit(0);  //end of main thread
    }
    
    0 讨论(0)
  • 2020-12-01 18:16

    You could start the threads detached, and not worry about joining.

    for (int i = 0; i < 2; i++) {
        pthread_t tid;
        pthread_create(&tid, NULL, routine, NULL);
        pthread_detach(tid);
    }
    pthread_exit(0);
    

    Or, alternatively, you can have the thread that dies report back to the main thread who it is, so that the threads are joined in the order they exited, rather than in the order you created them in.

    void *routine(void *arg)
    {
        int *fds = (int *)arg;
        pthread_t t = pthread_self();
        usleep((rand()/(1.0 + RAND_MAX)) * 1000000);
        write(fds[1], &t, sizeof(t));
    }
    
    int main()
    {
        int fds[2];
        srand(time(0));
        pipe(fds);
        for (int i = 0; i < 2; i++) {
            pthread_t tid;
            pthread_create(&tid, NULL, routine, fds);
            printf("created: %llu\n", (unsigned long long)tid);
        }
        for (int i = 0; i < 2; i++) {
            pthread_t tid;
            read(fds[0], &tid, sizeof(tid));
            printf("joining: %llu\n", (unsigned long long)tid);
            pthread_join(tid, 0);
        }
        pthread_exit(0);
    }
    
    0 讨论(0)
  • 2020-12-01 18:26
    int main()
    {
        pthread_t tid[2];
        for (int i = 0; i < 2; i++) {
            pthread_create(&tid[i], NULL, routine, NULL);
        }
        for (int i = 0; i < 2; i++)
           pthread_join(tid[i], NULL);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-01 18:31

    First create all the threads, then join all of them:

    pthread_t tid[2];
    
    /// create all threads
    for (int i = 0; i < 2; i++) {
        pthread_create(&tid[i], NULL, routine, NULL);
    }
    
    /// wait all threads by joining them
    for (int i = 0; i < 2; i++) {
        pthread_join(tid[i], NULL);  
    }
    

    Alternatively, have some pthread_attr_t variable, use pthread_attr_init(3) then pthread_attr_setdetachedstate(3) on it, then pass its address to pthread_create(3) second argument. Thos would create the threads in detached state. Or use pthread_detach as explained in Jxh's answer.

    0 讨论(0)
提交回复
热议问题