3 C++ Solutions: Promises, Condition_variables, Atomic Bools
If you liked it please upvote!\n\nC++ promise/future:\n1 of the 2 fastest (second one is the condition_variable)\n\nclass Foo {\nprivate:\n    std::promise<void
If you liked it please upvote!\n\n**C++ promise/future:**\n1 of the 2 fastest (second one is the condition_variable)\n```\nclass Foo {\nprivate:\n    std::promise<void> prom_first;\n    std::promise<void> prom_second;\n    std::future<void> fut_first;\n    std::future<void> fut_second;\npublic:\n    Foo() {\n        fut_first = prom_first.get_future();\n        fut_second = prom_second.get_future();\n    }\n    \n    void first(function<void()> printFirst) {\n        // printFirst() outputs "first". Do not change or remove this line.\n        printFirst();\n        prom_first.set_value();\n    }\n\n    void second(function<void()> printSecond) {\n        fut_first.wait();\n        // printSecond() outputs "second". Do not change or remove this line.\n        printSecond();\n        prom_second.set_value();\n    }\n\n    void third(function<void()> printThird) {\n        fut_second.wait();\n        // printThird() outputs "third". Do not change or remove this line.\n        printThird();\n    }\n};\n```\n\n**C++ condition variable**\n1 of the 2 fastest (second one is the promises/futures)\n```\nclass Foo {\nprivate:\n    std::mutex mutex1, mutex2, mutex3;\n    std::condition_variable cv2, cv3;\n    bool second_unlocked = false;\n    bool third_unlocked = false;\npublic:\n    Foo() { }\n    \n    void first(function<void()> printFirst) {\n        // printFirst() outputs "first". Do not change or remove this line.\n        printFirst();\n        {\n            std::lock_guard lock(mutex1);\n            second_unlocked = true;\n        }\n        cv2.notify_one();\n    }\n\n    void second(function<void()> printSecond) {\n        std::unique_lock lock(mutex2);\n        cv2.wait(lock, [this] { return second_unlocked; });\n        \n        // printSecond() outputs "second". Do not change or remove this line.\n        printSecond();\n        \n        third_unlocked = true;\n        lock.unlock();\n        cv3.notify_one();\n    }\n\n    void third(function<void()> printThird) {\n        std::unique_lock lock(mutex3);\n        cv3.wait(lock, [this] { return third_unlocked; });\n        \n        // printThird() outputs "third". Do not change or remove this line.\n        printThird();\n    }\n};\n```\n\n**C++ atomic bools**\nSimplest but obviously the slowest solution\n```\nclass Foo {\nprivate:\n    std::atomic<bool> second_unlocked = false, third_unlocked = false;\npublic:\n    Foo() { }\n    \n    void first(function<void()> printFirst) {\n        // printFirst() outputs "first". Do not change or remove this line.\n        printFirst();\n        second_unlocked = true;\n    }\n\n    void second(function<void()> printSecond) {\n        while(!second_unlocked)\n            ;\n        \n        // printSecond() outputs "second". Do not change or remove this line.\n        printSecond();\n        \n        third_unlocked = true;\n    }\n\n    void third(function<void()> printThird) {\n        \n        while(!third_unlocked)\n            ;\n        \n        // printThird() outputs "third". Do not change or remove this line.\n        printThird();\n    }\n};\n```
[]