究竟是什么join()方法中的boost ::线程? (C ++) [英] What exactly is join() in Boost::thread? (C++)

查看:197
本文介绍了究竟是什么join()方法中的boost ::线程? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我会做这样的事情:

In Java, I would do something like:

Thread t = new MyThread();
t.start();

我通过调用start()方法启动线程。所以,以后我可以这样做:

I start thread by calling start() method. So later I can do something like:

for (int i = 0; i < limit; ++i)
{
    Thread t = new MyThread();
    t.start();
}

要创建一组线程和执行运行code()方法。

To create a group of threads and execute the code in run() method.

不过,在C ++中,有作为start()方法没有这样的事情。使用Boost,如果我想要一个线程开始运行,我必须调用join()方法,以使一个线程在运行。

However, in C++, there's no such thing as start() method. Using Boost, if I want a thread to start running, I have to call the join() method in order to make a thread running.

#include <iostream>
#include <boost/thread.hpp>

class Worker
{
public:
    Worker() 
    {
        // the thread is not-a-thread until we call start()
    }

    void start(int N)
    {
        m_Thread = boost::thread(&Worker::processQueue, this, N);
    }

    void join()
    {
        m_Thread.join();
    }

    void processQueue(unsigned N)
    {
        float ms = N * 1e3;
        boost::posix_time::milliseconds workTime(ms);

        std::cout << "Worker: started, will work for "
                  << ms << "ms"
                  << std::endl;

        // We're busy, honest!
        boost::this_thread::sleep(workTime);
        std::cout << "Worker: completed" << std::endl;
    }

private:

    boost::thread m_Thread;
};

int main(int argc, char* argv[])
{
    std::cout << "main: startup" << std::endl;

    Worker worker, w2, w3, w5;

    worker.start(3);
    w2.start(3);
    w3.start(3);
    w5.start(3);

    worker.join();
    w2.join();
    w3.join();
    w5.join();
    for (int i = 0; i < 100; ++i)
    {
        Worker w;
        w.start(3);
        w.join();
    }
    //std::cout << "main: waiting for thread" << std::endl;    

    std::cout << "main: done" << std::endl;

    return 0;
}

在上面的code,for循环创建100个线程,通常我必须用一个boost :: thread_group添加线程函数,最后所有join_all的run()。但是,我不知道如何在其中使用不同类成员的类线程函数将做到这一点。

On the code above, the for loop to create 100 threads, normally I must use a boost::thread_group to add the thread function, and finally run all with join_all(). However, I don't know how to do it with thread function putting in a class which uses various class members.

在另一方面,上面的​​循环不会表现得像Java中的循环。这将使每个线程顺序执行,不要一次像其他分离线程,他自己加入()被调用。

On the other hand, the loop above will not behave like the loop in Java. It will make each thread execute sequentially, not all at once like the other separated threads, whose own join() is called.

什么是升压join()方法是什么呢?还请大家帮我创建一组共享同一个类的线程。

What is join() in Boost exactly? Also please help me to create a group of threads which share the same class.

推荐答案

加入不启动线程,那你直到线程要加入饰面块。您可以使用它时,你需要等待你开始完成其运行的线程。(例如 - 如果它计算的东西,你需要的结果)

join doesn't start the thread, it blocks you until the thread you're joining finishes. You use it when you need to wait for the thread you started to finish its run (for example - if it computes something and you need the result).

什么启动线程的boost ::线程,它创建线程,并打电话给你传递给它的线程函数(在你的案件 - 工人:: processQueue )。

What starts the thread is boost::thread, which creates the thread and calls the thread function you passed to it (in your case - Worker::processQueue).

您曾与循环的一个问题是不是因为线程没有启动,而是因为你的主线程没等他们完成之前执行的原因。 <罢>我猜你没有看到,因为调度差别在Java中这个问题,又名未定义行为。 修改后的 在Java线程的行为略有不同,请参见下面的评论的详细资料。这就解释了为什么你没有看到它在Java中

The reason you had a problem with the loop is not because the threads didn't start, but because your main thread didn't wait for them to execute before finishing. I'm guessing you didn't see this problem in Java because of the scheduling differences, aka "undefined behavior". after edit In Java the threading behaves slightly differently, see the comment below for details. That explains why you didn't see it in Java.

这里有一个关于的boost :: thread_group 。阅读问题的code和答案,它会帮助你。

Here's a question about the boost::thread_group. Read the code in the question and the answers, it will help you.

这篇关于究竟是什么join()方法中的boost ::线程? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆