{"id":118,"date":"2015-03-31T17:32:05","date_gmt":"2015-03-31T17:32:05","guid":{"rendered":""},"modified":"2015-03-31T17:32:05","modified_gmt":"2015-03-31T17:32:05","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=118","title":{"rendered":"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf"},"content":{"rendered":"<p>1.\u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cfpthread_cond_init<br \/>\n#include &lt;pthread.h&gt;<br \/>\nint pthread_cond_init(pthread_cond_t *cv,const pthread_condattr_t *cattr);<br \/>\n\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<br \/>\n\u521d \u59cb\u5316\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u53c2\u6570cattr\u4e3a\u7a7a\u6307\u9488\u65f6\uff0c\u51fd\u6570\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u7f3a\u7701\u7684\u6761\u4ef6\u53d8\u91cf\u3002\u5426\u5219\u6761\u4ef6\u53d8\u91cf\u7684\u5c5e\u6027\u5c06\u7531cattr\u4e2d\u7684\u5c5e\u6027\u503c\u6765\u51b3\u5b9a\u3002\u8c03\u7528 pthread_cond_init\u51fd\u6570\u65f6\uff0c\u53c2\u6570cattr\u4e3a\u7a7a\u6307\u9488\u7b49\u4ef7\u4e8ecattr\u4e2d\u7684\u5c5e\u6027\u4e3a\u7f3a\u7701\u5c5e\u6027\uff0c\u53ea\u662f\u524d\u8005\u4e0d\u9700\u8981cattr\u6240\u5360\u7528\u7684\u5185\u5b58\u5f00\u9500\u3002\u8fd9 \u4e2a\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u6761\u4ef6\u53d8\u91cf\u88ab\u5b58\u653e\u5728\u53c2\u6570cv\u6307\u5411\u7684\u5185\u5b58\u4e2d\u3002<br \/>\n\u53ef\u4ee5\u7528\u5b8fPTHREAD_COND_INITIALIZER\u6765\u521d\u59cb\u5316\u9759\u6001\u5b9a\u4e49\u7684\u6761\u4ef6\u53d8\u91cf\uff0c\u4f7f\u5176\u5177\u6709\u7f3a\u7701\u5c5e\u6027\u3002\u8fd9\u548c\u7528pthread_cond_init\u51fd\u6570\u52a8\u6001\u5206\u914d\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002\u521d\u59cb\u5316\u65f6\u4e0d\u8fdb\u884c\u9519\u8bef\u68c0\u67e5\u3002\u5982\uff1a<br \/>\npthread_cond_t cv = PTHREAD_COND_INITIALIZER;<br \/>\n\u4e0d\u80fd\u7531\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u521d\u59cb\u5316\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u9700\u8981\u91cd\u65b0\u521d\u59cb\u5316\u6216\u91ca\u653e\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u65f6\uff0c\u5e94\u7528\u7a0b\u5e8f\u5fc5\u987b\u4fdd\u8bc1\u8fd9\u4e2a\u6761\u4ef6\u53d8\u91cf\u672a\u88ab\u4f7f\u7528\u3002<\/p>\n<p>2.\u963b\u585e\u5728\u6761\u4ef6\u53d8\u91cf\u4e0apthread_cond_wait<br \/>\n#include &lt;pthread.h&gt;<br \/>\nint pthread_cond_wait(pthread_cond_t *cv,pthread_mutex_t *mutex);<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<br \/>\n\u51fd\u6570\u5c06\u89e3\u9501mutex\u53c2\u6570\u6307\u5411\u7684\u4e92\u65a5\u9501\uff0c\u5e76\u4f7f\u5f53\u524d\u7ebf\u7a0b\u963b\u585e\u5728cv\u53c2\u6570\u6307\u5411\u7684\u6761\u4ef6\u53d8\u91cf\u4e0a\u3002<br \/>\n\u88ab\u963b\u585e\u7684\u7ebf\u7a0b\u53ef\u4ee5\u88abpthread_cond_signal\u51fd\u6570\uff0cpthread_cond_broadcast\u51fd\u6570\u5524\u9192\uff0c\u4e5f\u53ef\u80fd\u5728\u88ab\u4fe1\u53f7\u4e2d\u65ad\u540e\u88ab\u5524\u9192\u3002<br \/>\npthread_cond_wait\u51fd\u6570\u7684\u8fd4\u56de\u5e76\u4e0d\u610f\u5473\u7740\u6761\u4ef6\u7684\u503c\u4e00\u5b9a\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u5fc5\u987b\u91cd\u65b0\u68c0\u67e5\u6761\u4ef6\u7684\u503c\u3002<br \/>\npthread_cond_wait\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u76f8\u5e94\u7684\u4e92\u65a5\u9501\u5c06\u88ab\u5f53\u524d\u7ebf\u7a0b\u9501\u5b9a\uff0c\u5373\u4f7f\u662f\u51fd\u6570\u51fa\u9519\u8fd4\u56de\u3002<br \/>\n\u4e00\u822c\u4e00\u4e2a\u6761\u4ef6\u8868\u8fbe\u5f0f\u90fd\u662f\u5728\u4e00\u4e2a\u4e92\u65a5\u9501\u7684\u4fdd\u62a4\u4e0b\u88ab\u68c0\u67e5\u3002\u5f53\u6761\u4ef6\u8868\u8fbe\u5f0f\u672a\u88ab\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u5c06\u4ecd\u7136\u963b\u585e\u5728\u8fd9\u4e2a\u6761\u4ef6\u53d8\u91cf\u4e0a\u3002\u5f53\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6539\u53d8\u4e86\u6761\u4ef6\u7684\u503c\u5e76\u5411\u6761\u4ef6\u53d8\u91cf\u53d1\u51fa\u4fe1\u53f7\u65f6\uff0c\u7b49\u5f85\u5728\u8fd9\u4e2a\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u4e00\u4e2a\u7ebf\u7a0b\u6216\u6240\u6709\u7ebf\u7a0b\u88ab\u5524\u9192\uff0c\u63a5\u7740\u90fd\u8bd5\u56fe\u518d\u6b21\u5360\u6709\u76f8\u5e94\u7684\u4e92\u65a5\u9501\u3002<br \/>\n\u963b\u585e\u5728\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u7ebf\u7a0b\u88ab\u5524\u9192\u4ee5\u540e\uff0c\u76f4\u5230pthread_cond_wait()\u51fd\u6570\u8fd4\u56de\u4e4b\u524d\u6761\u4ef6\u7684\u503c\u90fd\u6709\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u3002\u6240\u4ee5\u51fd\u6570\u8fd4\u56de\u4ee5\u540e\uff0c\u5728\u9501\u5b9a\u76f8\u5e94\u7684\u4e92 \u65a5\u9501\u4e4b\u524d\uff0c\u5fc5\u987b\u91cd\u65b0\u6d4b\u8bd5\u6761\u4ef6\u503c\u3002\u6700\u597d\u7684\u6d4b\u8bd5\u65b9\u6cd5\u662f\u5faa\u73af\u8c03\u7528pthread_cond_wait\u51fd\u6570\uff0c\u5e76\u628a\u6ee1\u8db3\u6761\u4ef6\u7684\u8868\u8fbe\u5f0f\u7f6e\u4e3a\u5faa\u73af\u7684\u7ec8\u6b62\u6761\u4ef6\u3002\u5982\uff1a<\/p>\n<p>pthread_mutex_lock();<br \/>\nwhile (condition_is_false)<br \/>\npthread_cond_wait();<br \/>\npthread_mutex_unlock();<\/p>\n<p>\u963b\u585e\u5728\u540c\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u4e0d\u540c\u7ebf\u7a0b\u88ab\u91ca\u653e\u7684\u6b21\u5e8f\u662f\u4e0d\u4e00\u5b9a\u7684\u3002<br \/>\n\u6ce8\u610f\uff1apthread_cond_wait()\u51fd\u6570\u662f\u9000\u51fa\u70b9\uff0c\u5982\u679c\u5728\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u65f6\uff0c\u5df2\u6709\u4e00\u4e2a\u6302\u8d77\u7684\u9000\u51fa\u8bf7\u6c42\uff0c\u4e14\u7ebf\u7a0b\u5141\u8bb8\u9000\u51fa\uff0c\u8fd9\u4e2a\u7ebf\u7a0b\u5c06\u88ab\u7ec8\u6b62\u5e76\u5f00\u59cb\u6267\u884c\u5584\u540e\u5904\u7406\u51fd\u6570\uff0c\u800c\u8fd9\u65f6\u548c\u6761\u4ef6\u53d8\u91cf\u76f8\u5173\u7684\u4e92\u65a5\u9501\u4ecd\u5c06\u5904\u5728\u9501\u5b9a\u72b6\u6001\u3002<br \/>\n&nbsp;<br \/>\n3.\u89e3\u9664\u5728\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u963b\u585epthread_cond_signal<br \/>\n#include &lt;pthread.h&gt;<br \/>\nint pthread_cond_signal(pthread_cond_t *cv);<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<br \/>\n\u51fd\u6570\u88ab\u7528\u6765\u91ca\u653e\u88ab\u963b\u585e\u5728\u6307\u5b9a\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u4e00\u4e2a\u7ebf\u7a0b\u3002<br \/>\n\u5fc5\u987b\u5728\u4e92\u65a5\u9501\u7684\u4fdd\u62a4\u4e0b\u4f7f\u7528\u76f8\u5e94\u7684\u6761\u4ef6\u53d8\u91cf\u3002\u5426\u5219\u5bf9\u6761\u4ef6\u53d8\u91cf\u7684\u89e3\u9501\u6709\u53ef\u80fd\u53d1\u751f\u5728\u9501\u5b9a\u6761\u4ef6\u53d8\u91cf\u4e4b\u524d\uff0c\u4ece\u800c\u9020\u6210\u6b7b\u9501\u3002<br \/>\n\u5524\u9192\u963b\u585e\u5728\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u6240\u6709\u7ebf\u7a0b\u7684\u987a\u5e8f\u7531\u8c03\u5ea6\u7b56\u7565\u51b3\u5b9a\uff0c\u5982\u679c\u7ebf\u7a0b\u7684\u8c03\u5ea6\u7b56\u7565\u662fSCHED_OTHER\u7c7b\u578b\u7684\uff0c\u7cfb\u7edf\u5c06\u6839\u636e\u7ebf\u7a0b\u7684\u4f18\u5148\u7ea7\u5524\u9192\u7ebf\u7a0b\u3002<br \/>\n\u5982\u679c\u6ca1\u6709\u7ebf\u7a0b\u88ab\u963b\u585e\u5728\u6761\u4ef6\u53d8\u91cf\u4e0a\uff0c\u90a3\u4e48\u8c03\u7528pthread_cond_signal()\u5c06\u6ca1\u6709\u4f5c\u7528\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>4.\u963b\u585e\u76f4\u5230\u6307\u5b9a\u65f6\u95f4pthread_cond_timedwait<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;time.h&gt;<br \/>\nint pthread_cond_timedwait(pthread_cond_t *cv,pthread_mutex_t *mp, const structtimespec * abstime);<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<br \/>\n\u51fd\u6570\u5230\u4e86\u4e00\u5b9a\u7684\u65f6\u95f4\uff0c\u5373\u4f7f\u6761\u4ef6\u672a\u53d1\u751f\u4e5f\u4f1a\u89e3\u9664\u963b\u585e\u3002\u8fd9\u4e2a\u65f6\u95f4\u7531\u53c2\u6570abstime\u6307\u5b9a\u3002\u51fd\u6570\u8fd4\u56de\u65f6\uff0c\u76f8\u5e94\u7684\u4e92\u65a5\u9501\u5f80\u5f80\u662f\u9501\u5b9a\u7684\uff0c\u5373\u4f7f\u662f\u51fd\u6570\u51fa\u9519\u8fd4\u56de\u3002<br \/>\n\u6ce8\u610f\uff1apthread_cond_timedwait\u51fd\u6570\u4e5f\u662f\u9000\u51fa\u70b9\u3002<br \/>\n\u8d85\u65f6\u65f6\u95f4\u53c2\u6570\u662f\u6307\u4e00\u5929\u4e2d\u7684\u67d0\u4e2a\u65f6\u523b\u3002\u4f7f\u7528\u4e3e\u4f8b\uff1a<br \/>\npthread_timestruc_t to;<br \/>\nto.tv_sec = time(NULL) + TIMEOUT;<br \/>\nto.tv_nsec = 0;<br \/>\n\u8d85\u65f6\u8fd4\u56de\u7684\u9519\u8bef\u7801\u662fETIMEDOUT\u3002<\/p>\n<p>5.\u91ca\u653e\u963b\u585e\u7684\u6240\u6709\u7ebf\u7a0bpthread_cond_broadcast<br \/>\n#include &lt;pthread.h&gt;<br \/>\nint pthread_cond_broadcast(pthread_cond_t *cv);<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<br \/>\n\u51fd\u6570\u5524\u9192\u6240\u6709\u88abpthread_cond_wait\u51fd\u6570\u963b\u585e\u5728\u67d0\u4e2a\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u7ebf\u7a0b\uff0c\u53c2\u6570cv\u88ab\u7528\u6765\u6307\u5b9a\u8fd9\u4e2a\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u6ca1\u6709\u7ebf\u7a0b\u963b\u585e\u5728\u8fd9\u4e2a\u6761\u4ef6\u53d8\u91cf\u4e0a\u65f6\uff0cpthread_cond_broadcast\u51fd\u6570\u65e0\u6548\u3002<br \/>\n\u7531\u4e8epthread_cond_broadcast\u51fd\u6570\u5524\u9192\u6240\u6709\u963b\u585e\u5728\u67d0\u4e2a\u6761\u4ef6\u53d8\u91cf\u4e0a\u7684\u7ebf\u7a0b\uff0c\u8fd9\u4e9b\u7ebf\u7a0b\u88ab\u5524\u9192\u540e\u5c06\u518d\u6b21\u7ade\u4e89\u76f8\u5e94\u7684\u4e92\u65a5\u9501\uff0c\u6240\u4ee5\u5fc5\u987b\u5c0f\u5fc3\u4f7f\u7528pthread_cond_broadcast\u51fd\u6570\u3002<\/p>\n<p>6.\u91ca\u653e\u6761\u4ef6\u53d8\u91cfpthread_cond_destroy<br \/>\n#include &lt;pthread.h&gt;<br \/>\nint pthread_cond_destroy(pthread_cond_t *cv);<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<br \/>\n\u6ce8\u610f\uff1a\u6761\u4ef6\u53d8\u91cf\u5360\u7528\u7684\u7a7a\u95f4\u5e76\u672a\u88ab\u91ca\u653e\u3002<\/p>\n<p>7.\u5524\u9192\u4e22\u5931\u95ee\u9898<br \/>\n\u5728\u7ebf\u7a0b\u672a\u83b7\u5f97\u76f8\u5e94\u7684\u4e92\u65a5\u9501\u65f6\u8c03\u7528pthread_cond_signal\u6216pthread_cond_broadcast\u51fd\u6570\u53ef\u80fd\u4f1a\u5f15\u8d77\u5524\u9192\u4e22\u5931\u95ee\u9898\u3002<br \/>\n\u5524\u9192\u4e22\u5931\u5f80\u5f80\u4f1a\u5728\u4e0b\u9762\u7684\u60c5\u51b5\u4e0b\u53d1\u751f\uff1a<br \/>\n&nbsp;&nbsp;&nbsp; \u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528pthread_cond_signal\u6216pthread_cond_broadcast\u51fd\u6570\uff1b<br \/>\n&nbsp;&nbsp;&nbsp; \u53e6\u4e00\u4e2a\u7ebf\u7a0b\u6b63\u5904\u5728\u6d4b\u8bd5\u6761\u4ef6\u53d8\u91cf\u548c\u8c03\u7528pthread_cond_wait\u51fd\u6570\u4e4b\u95f4\uff1b<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; \u6ca1\u6709\u7ebf\u7a0b\u6b63\u5728\u5904\u5728\u963b\u585e\u7b49\u5f85\u7684\u72b6\u6001\u4e0b\u3002\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t\u6d4b\u8bd5\u4ee3\u7801\uff1a\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;pthread.h&gt;\n<\/p>\n<p>\n\tpthread_cond_t cond;<br \/>\npthread_mutex_t mutex;\n<\/p>\n<p>\n\tint pthread_init()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; pthread_mutex_init(&amp;mutex,NULL);<br \/>\n&nbsp;&nbsp;&nbsp; pthread_cond_init(&amp;cond,NULL);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n}\n<\/p>\n<p>\n\tvoid *new_thread(void *Parameter)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; int n;<br \/>\n&nbsp;&nbsp;&nbsp; int num=*(int *)Parameter;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; while(1)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_lock(&amp;mutex);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8fdb\u5165\u6761\u4ef6\u53d8\u91cf\u65f6\u963b\u585e\u7ebf\u7a0b\uff0c\u91ca\u653e\u4e92\u65a5\u9501<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_cond_wait(&amp;cond,&amp;mutex);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u79bb\u5f00\u6761\u4ef6\u53d8\u91cf\u65f6\u83b7\u53d6\u4e92\u65a5\u9501<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_unlock(&amp;mutex);\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(n=0;n&lt;10;n++)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&#8220;I&#8217;m %d.\\n&#8221;,num);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep(1);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; }\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return NULL;<br \/>\n}\n<\/p>\n<p>\n\tint start_thread()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; pthread_mutex_lock(&amp;mutex);<br \/>\n&nbsp;&nbsp;&nbsp; \/\/\u91ca\u653e\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf<br \/>\n&nbsp;&nbsp;&nbsp; pthread_cond_signal(&amp;cond);<br \/>\n&nbsp;&nbsp;&nbsp; pthread_mutex_unlock(&amp;mutex);<br \/>\n&nbsp;&nbsp;&nbsp; return 0;<br \/>\n}\n<\/p>\n<p>\n\tint main()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; int n;<br \/>\n&nbsp;&nbsp;&nbsp; pthread_t threadID;<br \/>\n&nbsp;&nbsp;&nbsp; char ch;\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; pthread_init();<br \/>\n&nbsp;&nbsp;&nbsp; for(n=0;n&lt;10;n++)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_create(&amp;threadID,NULL,new_thread,(void *)&amp;n);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; usleep(1000);<br \/>\n&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; while(1)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fflush(stdin);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scanf(&#8220;%c&#8221;,&amp;ch);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch(ch)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#8216;r&#8217;:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; start_thread();<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case &#8216;a&#8217;:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_lock(&amp;mutex);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u91ca\u653e\u6240\u6709\u7684\u6761\u4ef6\u53d8\u91cf<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_cond_broadcast(&amp;cond);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pthread_mutex_unlock(&amp;mutex);<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; }\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp; return 0;<br \/>\n}\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cfpthread_cond_init<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>int pthread_cond_init(pthread_cond_t *cv,<\/p>\n<p>const pthread_condattr_t *cattr);<\/p>\n<p>\u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef<\/p>\n<p>\u521d \u59cb\u5316\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u53c2\u6570cattr\u4e3a\u7a7a\u6307\u9488\u65f6\uff0c\u51fd\u6570\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u7f3a\u7701\u7684\u6761\u4ef6\u53d8\u91cf&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[332],"tags":[],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=118\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"1.\u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cfpthread_cond_init  #include &lt;pthread.h&gt;  int pthread_cond_init(pthread_cond_t *cv,  const pthread_condattr_t *cattr);  \u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef  \u521d \u59cb\u5316\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u53c2\u6570cattr\u4e3a\u7a7a\u6307\u9488\u65f6\uff0c\u51fd\u6570\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u7f3a\u7701\u7684\u6761\u4ef6\u53d8\u91cf...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=118\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-31T17:32:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=118#webpage\",\"url\":\"http:\/\/weizn.net\/?p=118\",\"name\":\"Linux\\u7ebf\\u7a0b\\uff1a\\u6761\\u4ef6\\u53d8\\u91cf - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"datePublished\":\"2015-03-31T17:32:05+00:00\",\"dateModified\":\"2015-03-31T17:32:05+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=118#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=118\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=118#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\\u7ebf\\u7a0b\\uff1a\\u6761\\u4ef6\\u53d8\\u91cf\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=118#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=118#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"Linux\\u7ebf\\u7a0b\\uff1a\\u6761\\u4ef6\\u53d8\\u91cf\",\"datePublished\":\"2015-03-31T17:32:05+00:00\",\"dateModified\":\"2015-03-31T17:32:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=118#webpage\"},\"wordCount\":702,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"articleSection\":[\"Linux\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=118#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=118","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf - Wayne&#039;s Blog","og_description":"1.\u521d\u59cb\u5316\u6761\u4ef6\u53d8\u91cfpthread_cond_init  #include &lt;pthread.h&gt;  int pthread_cond_init(pthread_cond_t *cv,  const pthread_condattr_t *cattr);  \u8fd4\u56de\u503c\uff1a\u51fd\u6570\u6210\u529f\u8fd4\u56de0\uff1b\u4efb\u4f55\u5176\u4ed6\u8fd4\u56de\u503c\u90fd\u8868\u793a\u9519\u8bef  \u521d \u59cb\u5316\u4e00\u4e2a\u6761\u4ef6\u53d8\u91cf\u3002\u5f53\u53c2\u6570cattr\u4e3a\u7a7a\u6307\u9488\u65f6\uff0c\u51fd\u6570\u521b\u5efa\u7684\u662f\u4e00\u4e2a\u7f3a\u7701\u7684\u6761\u4ef6\u53d8\u91cf...","og_url":"http:\/\/weizn.net\/?p=118","og_site_name":"Wayne&#039;s Blog","article_published_time":"2015-03-31T17:32:05+00:00","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=118#webpage","url":"http:\/\/weizn.net\/?p=118","name":"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"datePublished":"2015-03-31T17:32:05+00:00","dateModified":"2015-03-31T17:32:05+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=118#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=118"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=118#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=118#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=118#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"Linux\u7ebf\u7a0b\uff1a\u6761\u4ef6\u53d8\u91cf","datePublished":"2015-03-31T17:32:05+00:00","dateModified":"2015-03-31T17:32:05+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=118#webpage"},"wordCount":702,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"articleSection":["Linux"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=118#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=118"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}