c++ efficient & clean solution using 2 semaphore
// we have to include the semaphore header file ,as c++ 20 is not available in our LC editor currently\n\n\n#include <semaphore.h>\nclass FooBar {\nprivate:\n  
// we have to include the semaphore header file ,as c++ 20 is not available in our LC editor currently\n\n```\n#include <semaphore.h>\nclass FooBar {\nprivate:\n    int n;\n    sem_t foo_sem;\n    sem_t bar_sem;\n    \npublic:\n    FooBar(int n) {\n        this->n = n;\n        sem_init(&foo_sem, 0, 1);\n        sem_init(&bar_sem, 0, 0);\n    }\n    \n    ~FooBar() {\n        sem_destroy(&foo_sem);\n        sem_destroy(&bar_sem);\n    }\n\n    void foo(function<void()> printFoo) {\n        \n        for (int i = 0; i < n; i++) {\n                // lock foo semaphore\n            sem_wait(&foo_sem); \n        \t    // printFoo() outputs "foo". Do not change or remove this line.\n        \tprintFoo();\n                // unlock bar semaphore\n            sem_post(&bar_sem); \n        }\n    }\n\n    void bar(function<void()> printBar) {\n        \n        for (int i = 0; i < n; i++) {\n                // lock bar semaphore\n            sem_wait(&bar_sem); \n        \t    // printBar() outputs "bar". Do not change or remove this line.\n        \tprintBar();\n                // unlock foo semaphore\n            sem_post(&foo_sem); \n        }\n    }\n};\n```
['C']