c 0ms 100%
\n\ntypedef struct\n{\n    // User defined data may be declared here.\n    int turn;\n    pthread_mutex_t lock;\n    pthread_cond_t cond;\n} Foo;\n\nFoo* fooCre
![image](https://assets.leetcode.com/users/images/65577e65-fc51-4d55-a6ff-ad0c635d4717_1666065469.4353917.png)\n```\ntypedef struct\n{\n    // User defined data may be declared here.\n    int turn;\n    pthread_mutex_t lock;\n    pthread_cond_t cond;\n} Foo;\n\nFoo* fooCreate()\n{\n    Foo* obj = (Foo*) malloc(sizeof(Foo));\n    // Initialize user defined data here.\n    // init mutex and cond\n    pthread_mutex_init(&(obj->lock), NULL);\n    pthread_cond_init(&(obj->cond), NULL);\n    obj->turn = 1;\n    return obj;\n}\n\nvoid first(Foo* obj)\n{\n    // take lock\n    pthread_mutex_lock(&(obj->lock));\n    // while not my turn\n    while(obj->turn != 1)\n    {\n        // wait and unlock\n        pthread_cond_wait(&(obj->cond), &(obj->lock)); \n    }\n    // printFirst() outputs "first". Do not change or remove this line.\n    printFirst();\n    obj->turn = 2;\n    // unlock\n    pthread_mutex_unlock(&(obj->lock));\n    // broadcast\n    pthread_cond_broadcast(&(obj->cond));\n}\n\nvoid second(Foo* obj)\n{\n    pthread_mutex_lock(&(obj->lock)); \n    while(obj->turn != 2)\n    {\n        pthread_cond_wait(&(obj->cond), &(obj->lock));\n    }\n    // printSecond() outputs "second". Do not change or remove this line.\n    printSecond();\n    obj->turn = 3;\n    pthread_mutex_unlock(&(obj->lock));\n    pthread_cond_broadcast(&(obj->cond));\n}\n\nvoid third(Foo* obj)\n{    \n    pthread_mutex_lock(&(obj->lock)); \n    while(obj->turn != 3)\n    {\n        pthread_cond_wait(&(obj->cond), &(obj->lock)); \n    }\n    // printThird() outputs "third". Do not change or remove this line.\n    printThird();\n    obj->turn = 1;\n    pthread_mutex_unlock(&(obj->lock));\n    pthread_cond_broadcast(&(obj->cond));\n}\n\nvoid fooFree(Foo* obj)\n{\n    // User defined data may be cleaned up here.\n    free(obj);\n}\n```\n\u2B06\uFE0Fplease upvote if helpful :)\u2B06\uFE0F
['C']