posix thread C solution
the similar problem like 1114,using posix thread tools to solve this\n\n#include <pthread.h>\n\n/*\n  1.we need two thread do not interrupt with each other\n   
the similar problem like 1114,using posix thread tools to solve this\n```\n#include <pthread.h>\n\n/*\n  1.we need two thread do not interrupt with each other\n    means when one thread is outputing, the other one don\'t do that\n  2.we have the preference in 1\'s basis, one need to do first \n  ====> for mutual exclusive, need a mutex,also a condition variable\n             to save cpu cycles\n\t\t\t because this mutual exclusive has some kind of "preference",\n\t\t\t so setup a global variable to tell two thread,who can print now\n\n*/\npthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\npthread_cond_t cond = PTHREAD_COND_INITIALIZER;\nint who=1;\ntypedef struct {\n    int n;\n} FooBar;\n\nFooBar* fooBarCreate(int n) {\n    FooBar* obj = (FooBar*) malloc(sizeof(FooBar));\n    obj->n = n;\n    return obj;\n}\n\nvoid foo(FooBar* obj) {\n    \n    for (int i = 0; i < obj->n; i++) {\n        pthread_mutex_lock(&mutex);\n        // printFoo() outputs "foo". Do not change or remove this line.\n        while(who==2){\n            pthread_cond_wait(&cond,&mutex);\n        }\n        printFoo();\n        who=2;\n        pthread_cond_broadcast(&cond);\n        pthread_mutex_unlock(&mutex);\n    }\n}\n\nvoid bar(FooBar* obj) {\n    \n    for (int i = 0; i < obj->n; i++) {\n        pthread_mutex_lock(&mutex);\n        // printBar() outputs "bar". Do not change or remove this line.\n        while(who==1){\n            pthread_cond_wait(&cond,&mutex);\n        }\n        printBar();\n        who=1;\n        pthread_cond_broadcast(&cond);\n        pthread_mutex_unlock(&mutex);\n    }\n}\n\nvoid fooBarFree(FooBar* obj) {\n    \n}\n```
[]