0

So I want to wait until ender has started waiting here is what basically:

        std::condition_variable avalanche;
        std::mutex mutex;
        std::cout << "avalanche" << std::endl;
        std::thread ender{[&]{
            std::unique_lock lock{mutex};
            avalanche.wait(lock);
        }};
        //Here how to wait until ender has started waiting on the
        //Conditional Variable

I just can't wrap my head around it.

My goal ultimately is to create a bunch of threads which will do some work on their own but then continue in the order of creation.

1
  • Can you explain the problem with more clarity? What are you trying to do and how does this code attempt to solve the problem? Commented Jan 28, 2022 at 18:33

1 Answer 1

0

If i understanding your question correctly, my solution is to use another sync component to let the outer thread wait till the ender thread send signal through this component.

If you want precisely let the outer thread wait until ender has started waiting, you should let outer waiting on the same mutex(then until ender release the lock through wait, outer can acquire the lock). Though this is considered as pessimization(one should not let the notifying thread holding the lock when it do the notify job), but i am just focusing on your tiny example and has no much context acknowledge about the real situation.

std::condition_variable avalanche;
std::mutex mutex;
std::cout << "avalanche" << std::endl;
std::thread ender{[&]{
    std::unique_lock<std::mutex> lock{mutex};
    avalanche.notify_all();
    avalanche.wait(lock);
}};
{
    // lock scope
    std::unique_lock<std::mutex> lock{mutex};
    avalanche.wait(lock);
}
// do something
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.