简体   繁体   English

停止主线程,直到所有其他线程完成

[英]Stop main thread until all the other threads finishes

I want to stop the main thread until all the other threads finishes running.我想停止主线程,直到所有其他线程完成运行。 Is there any possible way to do that using pthreads in C?在 C 中使用 pthreads 有什么可能的方法吗? Is there any possible way to sleep the main thread?有没有办法让主线程休眠?

Here is the code I used to test.这是我用来测试的代码。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>

int a = 4;

void *func(void *arg)
{
    for (int i = 0; i < 5; i++)
    {
        sleep(1);
        printf("MY TURN : %d\n", i);
    }
}

void *func_2(void *arg)
{
    for (int i = 0; i < 5; i++)
    {
        sleep(1);
        printf("YOUR TURN : %d\n", i);
    }
}

void turn()
{
    for (int i = 0; i < 5; i++)
    {
        sleep(1);
        printf("THEIR TURN : %d \n", i);
    }
}

int main()
{
    pthread_t t_id, t_id_2;

    pthread_create(&t_id, NULL, func, NULL);
    pthread_create(&t_id_2, NULL, func_2, NULL);

    turn();

    pthread_join(t_id, NULL);
    pthread_join(t_id_2, NULL);
}

Output of the code Output的代码

THEIR TURN : 0
YOUR TURN : 0
MY TURN : 0
MY TURN : 1
THEIR TURN : 1
YOUR TURN : 1
MY TURN : 2
THEIR TURN : 2
YOUR TURN : 2
MY TURN : 3
THEIR TURN : 3
YOUR TURN : 3
MY TURN : 4
THEIR TURN : 4
YOUR TURN : 4

I want to run the turn() (function which is in main function) after running t_id and t_id_2 threads.我想在运行 t_id 和 t_id_2 线程后运行 turn() (主函数中的函数)。

What you want is actually exactly what the pthread_join function does.您想要的实际上正是pthread_join function 所做的。

From man pthread_join :man pthread_join

int pthread_join(pthread_t thread, void **retval);
The pthread_join() function waits for the thread specified by <thread> to terminate.
If that thread has already terminated, then pthread_join() returns immediately.
The thread specified by <thread> must be joinable.

So in your main function you can just join the threads before calling turn :因此,在您的main function 中,您可以在调用turn之前加入线程:

pthread_t t_id, t_id_2;

pthread_create(&t_id, NULL, func, NULL);
pthread_create(&t_id_2, NULL, func_2, NULL);

pthread_join(t_id, NULL);
pthread_join(t_id_2, NULL);

turn();

return 0;

That way turn is called after the thread functions finish their work.这样,在线程函数完成工作后调用turn

The classic way to wait for a thread is join() or pthread_join(), and that means the join caller must know all the thread ids, and the thread subroutines must return (terminate).等待线程的经典方法是 join() 或 pthread_join(),这意味着连接调用者必须知道所有线程 ID,并且线程子例程必须返回(终止)。

As thread pools and multiple pass problems came into vogue, non-terminal signalling like barrier() or pthread_barrier_init()/pthread_barrier_wait() became more common.随着线程池和多通道问题的流行,barrier() 或 pthread_barrier_init()/pthread_barrier_wait() 等非终端信号变得越来越普遍。 The latter is a bit like a semaphore, with a thread count but also with one 'master' thread getting a unique return.后者有点像信号量,具有线程数,但也有一个“主”线程获得唯一的回报。 In practice, barriers are often used in pairs, one for "are we all done this pass" with the unique 'master' thread delivering the result of the pass and another barrier to hold up the rest until the result is delivered by the 'master' thread before the problem pass variables can be modified in the next pass.在实践中,屏障通常成对使用,一个用于“我们都完成了这一遍”,独特的“主”线程提供通过结果,另一个屏障用于支撑 rest 直到结果由“主”传递' 问题传递变量之前的线程可以在下一次传递中进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM