3

pthreads has undefined behavior if multiple threads try to join the same thread:

If multiple threads simultaneously try to join with the same thread, the results are undefined.

Is the same true for boost::threads? The documentation does not appears to specify this.

If it is undefined, then what would be a clean way for multiple threads to wait on one thread completing?

4
  • Hmm, interestingly, it is not mentioned in the N4296 C++ standard draft that I can read. I would say it is then UB and/or "don't do it" kind of behaviour. :)
    – wilx
    Feb 5, 2016 at 0:35
  • 1
    Other threads could test if a thread was joinable(), or the closing thread could use condition_variable to signal all its work is finished (even though it may be still running)
    – Tas
    Feb 5, 2016 at 0:50
  • 2
    @wilx, std::thread::join() is a non-const function, therefore calling it concurrently is a data race, i.e. undefined behaviour. That rule applies to all standard library types unless specified otherwise. Feb 5, 2016 at 1:26
  • @Tas, if two threads call joinable() concurrently what is to stop both of them then trying to call join()? Even if only one calls join(), without some kind of synchronization that join() call conflicts with the joinable() call in the other thread, leading to a data race, i.e. undefined behaviour. Feb 5, 2016 at 1:27

3 Answers 3

2

If it is undefined, then what would be a clean way for multiple threads to wait on one thread completing?

The clean way would be for that one thread to inform the others that it is complete. A packaged_task contains a future which can be waited on, which can help us here.

Here's one way of doing that. I have used std::thread and std::packaged_task, but you could use the boost equivalents just as well.

#include <thread>
#include <mutex>
#include <future>
#include <vector>
#include <iostream>

void emit(const char* msg) {
    static std::mutex m;
    std::lock_guard<std::mutex> l(m);
    std::cout << msg << std::endl;
    std::cout.flush();
}

int main()
{
    using namespace std;

    auto one_task = std::packaged_task<void()>([]{
        emit("waiting...");
        std::this_thread::sleep_for(std::chrono::microseconds(500));
        emit("wait over!");
    });

    // note: convert future to a shared_future so we can pass it
    // to two subordinate threads simultaneously
    auto one_done = std::shared_future<void>(one_task.get_future());
    auto one = std::thread(std::move(one_task));

    std::vector<std::thread> many;
    many.emplace_back([one_done] {
        one_done.wait();
        // do my thing here
        emit("starting thread 1");
    });

    many.emplace_back([one_done] {
        one_done.wait();
        // do my thing here
        emit("starting thread 2");
    });

    one.join();
    for (auto& t : many) {
        t.join();
    }

    cout << "Hello, World" << endl;
    return 0;
}

expected output:

waiting...
wait over!
starting thread 2
starting thread 1
Hello, World
0

I ended up using a boost::condition_variable... roughly:

class thread_wrapper {
    boost::mutex mutex;
    boost::condition_variable thread_done_condition;
    bool thread_done = false;

    void the_func() {
        // ...
        // end of the thread
        {  
            boost:unique_lock<boost::mutex> lock(mutex);
            thread_done = true;
        }
        thread_done_condition.notify_all();
    }

    void wait_until_done() {
        boost::unique_lock<boost::mutex> lock(mutex);
        thread_done_condition.wait(lock, [this]{ return thread_done; });
    }
}

Then multiple callers can safely call wait_until_done().

0

It strikes me now that something like the following would also have worked:

class thread_wrapper {
public:
    thread_wrapper() : thread([this]() { this->the_func(); }) { }

    void wait_until_done() {
        boost::unique_lock<boost::mutex> lock(join_mutex);
        thread.join();
    }

private:
    void the_func() {
        // ...
    }

    boost::mutex join_mutex;
    boost::thread thread;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.