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

后端 未结 4 1023
梦毁少年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: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;
    }
    

提交回复
热议问题