Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 something like:

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

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

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;
}

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.

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.

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

like image 816
Amumu Avatar asked Jun 05 '11 08:06

Amumu


People also ask

What is join in thread C++?

Joining means one thread will run until it reaches a certain point and then halts and waits for another thread to finish its execution (to its end); before it continues its own execution. At the point where the first thread halts, there is a join statement.

What is thread join ()?

The join() method of thread class waits for a thread to die. It is used when you want one thread to wait for completion of another. This process is like a relay race where the second runner waits until the first runner comes and hand over the flag to him.

What happens if you don't join a thread in C?

If you don't join these threads, you might end up using more resources than there are concurrent tasks, making it harder to measure the load. To be clear, if you don't call join , the thread will complete at some point anyway, it won't leak or anything.

Do I have to join a thread?

no, you can detach one thread if you want it to leave it alone. If you start a thread, either you detach it or you join it before the program ends, otherwise this is undefined behaviour.


2 Answers

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.

like image 163
littleadv Avatar answered Sep 24 '22 21:09

littleadv


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.

like image 34
Chris Jester-Young Avatar answered Sep 24 '22 21:09

Chris Jester-Young