多个线程可以加入同一个提升::线程吗?

Can multiple threads join the same boost::thread?

本文关键字:线程 同一个      更新时间:2023-10-16

如果多个线程尝试加入同一线程,pthreads具有未定义的行为:

如果多个线程同时尝试与同一线程连接, 结果未定义。

boost::thread也是如此吗?文档似乎没有指定这一点。

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

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

干净的方法是让一个线程通知其他线程它是完整的。packaged_task包含一个可以等待的future,这可以在这里帮助我们。

这是一种方法。我使用了std::thread和std::p ackaged_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

我最终使用了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()

现在我感到震惊的是,类似以下内容也可以工作:

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