多个线程可以加入相同的boost :: thread吗?

时间:2016-02-05 00:10:18

标签: c++ multithreading boost

如果多个线程尝试加入同一个线程,则

pthreads具有未定义的行为:

  

如果多个线程同时尝试使用同一个线程加入,          结果未定义。

boost::thread的情况是否一样?该文档似乎没有指定这一点。

如果未定义,那么多个线程在一个线程完成时等待的干净方式是什么?

3 个答案:

答案 0 :(得分:2)

  

如果未定义,那么多个线程在一个线程完成时等待的干净方式是什么?

干净的方式是为那一个线程通知其他人它已完成。 packaged_task包含可以等待的future,这可以帮助我们。

这是一种方法。我使用过std :: thread和std :: packaged_task,但你也可以使用boost等价物。

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

预期产出:

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

答案 1 :(得分:0)

我最终使用boost::condition_variable ...大致:

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

然后多个呼叫者可以安全地呼叫wait_until_done()

答案 2 :(得分:0)

现在让我感到震惊的是,以下内容也会起作用:

${project.build.directory}/dest/