🍞🍞🍞 Only used vector, no lock/semaphore/low-level objects
Approach\nNot a fan of low level stuff (even for easy questions), so here\'s a solution using nothing but a vector.\n\nI made the class have a vector, printed, 
# Approach\nNot a fan of low level stuff (even for easy questions), so here\'s a solution using nothing but a vector.\n\nI made the class have a vector, printed, which contains bool values of whether any given print function executed. Since the same instance will be passed to each thread, all threads can update this vector so that other threads can see.\n\nAdditonally, since each thread calls a different function (Thread A calls first(), Thread B second(), Thread C third()), they will all manipulate a different section of the Foo object (Thread A printed[0], Thread B printed[1], Thread C printed[2]), so race conditions are taken into account in this solution.\n\nThis solution has a high run time (beats 9.36% of solutions), and is not anything remotely like any reasonable expected solution. This solution is for the software engineers that ventured into the land of concurrency out of curiosity, like me.\n\n\n# Code\n```\nclass Foo {\nprivate:\n    vector<bool> printed; // keeps track of which print funcs executed\npublic:\n    Foo() : printed(3, false) {}\n\n    void first(function<void()> printFirst) {\n        printFirst(); // this should always print first\n        printed[0] = true;\n    }\n\n    void second(function<void()> printSecond) {\n        while (!printed[0]) {} // second will never print first, wait for first\n        printSecond();\n        printed[1] = true;\n    }\n\n    void third(function<void()> printThird) {\n        // no need to explicitly wait for first, since second should never execute before first\n        // besides, waiting for first and second creates a race condition, since two threads are reading the same piece of data\n        while (!printed[1]) {} // wait for second\n        printThird();\n        printed[2] = true;\n    }\n};\n```\nUPVOTE if this was helpful \uD83C\uDF5E\uD83C\uDF5E\uD83C\uDF5E
['Array', 'Design', 'Concurrency', 'C++']