mercredi 20 juillet 2016

Correct way to wait a condition variable that is notified by several threads

I'm trying to do this with the C++11 concurrency support.

I have a sort of thread pool of worker threads that all do the same thing, where a master thread has an array of condition variables (one for each thread, they need to 'start' synchronized, ie not run ahead one cycle of their loop).

    for (auto &worker_cond : cond_arr) {
        worker_cond.notify_one();
    }

then this thread has to wait for a notification of each thread of the pool to restart its cycle again. Whats the correct way of doing this? Have a single condition variable and wait on some integer each thread that isn't the master is going to increase? something like (still in the master thread)

    unique_lock<std::mutex> lock(workers_mtx);
    workers_finished.wait(lock, [&workers] { return workers = cond_arr.size(); });

Aucun commentaire:

Enregistrer un commentaire