🔥 All Language in Leetcode Solution with Mutex
C++\n\n#include<semaphore.h>\n\nclass FooBar {\nprivate:\n    int n;\n    sem_t F, B;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n        sem_init(&F
***C++***\n```\n#include<semaphore.h>\n\nclass FooBar {\nprivate:\n    int n;\n    sem_t F, B;\n\npublic:\n    FooBar(int n) {\n        this->n = n;\n        sem_init(&F, 0, 0);\n        sem_init(&B, 0, 1);\n    }\n\n    void foo(function<void()> printFoo) {\n        for (int i = 0; i < n; i++) {\n            sem_wait(&B);\n        \tprintFoo();\n            sem_post(&F);\n        }\n    }\n\n    void bar(function<void()> printBar) {\n        for (int i = 0; i < n; i++) {\n            sem_wait(&F);\n            printBar();\n            sem_post(&B);\n        }\n    }\n};\n```\n\n***Java***\n```\nclass FooBar {\n    private int n;\n\n    private Semaphore F = new Semaphore(1);\n    private Semaphore B = new Semaphore(0);\n\n    public FooBar(int n) {\n        this.n = n;\n    }\n\n    public void foo(Runnable printFoo) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            F.acquire();\n        \tprintFoo.run();\n            B.release();\n        }\n    }\n\n    public void bar(Runnable printBar) throws InterruptedException {\n        for (int i = 0; i < n; i++) {\n            B.acquire();\n        \tprintBar.run();\n            F.release();\n        }\n    }\n}\n```\n\n***Python***\n```\nimport threading\nclass FooBar(object):\n    def __init__(self, n):\n        self.n = n\n        self.F = threading.Semaphore(1)\n        self.B = threading.Semaphore(0)\n\n    def foo(self, printFoo):\n        for i in xrange(self.n):\n            self.F.acquire()\n            printFoo()\n            self.B.release()\n\n    def bar(self, printBar):\n        for i in xrange(self.n):\n            self.B.acquire()\n            printBar()\n            self.F.release()\n```\n\n***python3***\n```\nimport threading \nclass FooBar:\n    def __init__(self, n):\n        self.n = n\n        self.F = threading.Semaphore(1)\n        self.B = threading.Semaphore(0)\n\n    def foo(self, printFoo: \'Callable[[], None]\') -> None:\n        for i in range(self.n):\n            self.F.acquire()\n            printFoo()\n            self.B.release()\n\n    def bar(self, printBar: \'Callable[[], None]\') -> None:\n        for i in range(self.n):\n            self.B.acquire()\n            printBar()\n            self.F.release()\n```\n\n***C***\n```\ntypedef struct {\n    int n;\n    sem_t F;\n    sem_t B;\n} FooBar;\n\nFooBar* fooBarCreate(int n) {\n    FooBar* obj = (FooBar*) malloc(sizeof(FooBar));\n    obj->n = n;\n    sem_init(&(obj->F), 0, 0);\n    sem_init(&(obj->B), 0, 1);\n    return obj;\n}\n\nvoid foo(FooBar* obj) {\n    for (int i = 0; i < obj->n; i++) {\n        sem_wait(&(obj->B));\n        printFoo();\n        sem_post(&(obj->F));\n    }\n}\n\nvoid bar(FooBar* obj) {\n    for (int i = 0; i < obj->n; i++) {\n        sem_wait(&(obj->F));\n        printBar();\n        sem_post(&(obj->B));\n    }\n}\n\nvoid fooBarFree(FooBar* obj) {\n    sem_destroy(&(obj->B));\n    sem_destroy(&(obj->F));\n    free(obj);\n}\n```\n\n***C#***\n```\nusing System.Threading;\npublic class FooBar {\n     private int n;\n        private SemaphoreSlim F = new SemaphoreSlim(1);\n        private SemaphoreSlim B = new SemaphoreSlim(0);\n\n        public FooBar(int n)\n        {\n            this.n = n;\n        }\n\n        public void Foo(Action printFoo)\n        {\n            for (int i = 0; i < n; i++)\n            {\n                F.Wait();\n                printFoo();\n                B.Release();\n            }\n        }\n\n        public void Bar(Action printBar)\n        {\n            for (int i = 0; i < n; i++)\n            {\n                B.Wait();\n                printBar();\n                F.Release();\n            }\n        }\n}\n```
['C', 'Python', 'Java', 'Python3']