What exactly is join() in Boost::thread? (C++)

后端 未结 2 1627
无人及你
无人及你 2020-12-16 18:26

In Java, I would do something like:

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

I start thread by calling start() method. So later I can do somet

相关标签:
2条回答
  • 2020-12-16 18:49

    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).

    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).

    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.

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

    0 讨论(0)
  • 2020-12-16 19:02

    Joining a thread does the same thing in Boost as it does in Java: it waits for the thread to finish running.

    Plus, if I remember correctly, Boost's threads run upon construction. You don't start them explicitly.

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