简体   繁体   English

C ++ 11 std :: thread以单独的方法连接到主线程

[英]C++11 std::thread join to main thread in a separate method

I have the following code: 我有以下代码:

void do_join(std::thread& t)
{
    t.join();
}

void join_all(std::vector<std::thread>& v)
{
    std::for_each(v.begin(),v.end(),do_join);
}

int main()
{         
    std::vector<std::thread> myThreads;

    for(int i = 1; i <= 3; i++)
    {

        myThreads.push_back(std::thread(threadMethod));

    }


    join_all(myThreads);
}

The goal is to create multiple threads in a loop, add them to a thread vector and then iterating through the vector join them to the main thread. 目标是在一个循环中创建多个线程,将它们添加到线程向量中 ,然后遍历该向量将它们连接到线程。

The problem here is that when my do_join method executes for the first time it joins the thread and waits, not joining any other threads from a vector . 这里的问题是,当我的do_join方法首次执行时,它会加入线程并等待,而不是从vector中加入任何其他线程。 That is because my threads are using some conditional variables and waiting for some other tasks to complete. 那是因为我的线程正在使用一些条件变量,并等待其他一些任务完成。 Seems like that do_join method is waiting for just joined thread to complete. 好像do_join方法正在等待刚刚加入的线程完成。

The same thing happens if I try to do for_each directly in the main function. 如果我尝试直接在函数中执行for_each,则会发生相同的事情。

The idea is to be able to join all these threads to the main thread, not to that let's say do_join 's method thread which I suppose happened here. 这个想法是要能够将所有这些线程连接到线程,而不是让我假设do_join的方法线程发生在这里。 I could of course join and create them separately, because actually I don't need them to be in a vector (the number of threads is known from the beginning), but I need a vector because each thread in my application is actually created using different method's parameters which I did not included in that sample code. 我当然可以参加,并分别创建它们,因为实际上我并不需要他们在一个载体(线程数从一开始就知道),但我需要一个载体,因为在我的应用程序的每个线程使用实际创建我没有包含在该示例代码中的不同方法的参数。 I just do not want a new line for every single thread being created and joined. 我只是不想为每个创建和加入的线程添加新行。

Thank you for any help! 感谢您的任何帮助!

Edit: Maybe worth mentioning is that I'm using Ubuntu. 编辑:也许值得一提的是,我正在使用Ubuntu。

The join method by definition blocks current thread until the one you are trying to join is done: 根据定义join方法会阻塞当前线程,直到您尝试加入的线程完成为止:

Blocks the current thread until the thread identified by *this finishes its execution. 阻塞当前线程,直到由*this标识的线程完成执行。

( From here. ) 从这里开始。

That is, it's the purpose of join to block its thread until the other one finishes. 也就是说,它的目的 join挡住了它的线程,直到另一个结束。 If you don't want the thread to be blocked, then don't use join . 如果您不希望该线程被阻塞,则不要使用join

You should ask yourself the question: what are you trying to achieve? 您应该问自己一个问题:您要达到什么目标? If you want your main program to proceed only when all the other threads are done, then what you are doing now is right, you'll have to wait for all the threads anyway. 如果您希望主程序仅在所有其他线程都完成后才继续运行,那么您现在正在做的事情是正确的,无论如何,您都必须等待所有线程。 Otherwise you might need some way for other threads to signal the main one that they are done. 否则,您可能需要某种方式让其他线程向主要线程发出信号,告知它们已完成。

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

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