{"id":138,"date":"2011-09-23T17:06:42","date_gmt":"2011-09-23T09:06:42","guid":{"rendered":"http:\/\/hymike.net\/blog\/?p=138"},"modified":"2011-09-23T17:06:42","modified_gmt":"2011-09-23T09:06:42","slug":"%e8%a7%a3%e5%86%b3linux%e4%b8%8bwaitformultipleobjects%e7%9a%84%e4%b8%80%e7%a7%8d%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.hymike.net\/?p=138","title":{"rendered":"\u89e3\u51b3linux\u4e0bWaitForMultipleObjects\u7684\u4e00\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p>\u501f\u52a9\u4e86linux\u4e0b\u7684Semaphore\uff08\u4fe1\u53f7\u91cf\uff09\uff0c\u5b9e\u73b0\u4e86\u4e00\u79cd\u7b80\u5355\u7684WaitForMultipleObjects\u3002<br \/>\n\u5173\u4e8eSemaphore\u53c2\u8003\u5982\u4e0b\u7684\u6587\u7ae0\uff1a<br \/>\nhttp:\/\/blog.163.com\/wulin_shenghua\/blog\/static\/27580770200952644311771\/<\/p>\n<p>\u5177\u4f53\u601d\u60f3\uff1a\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u4e00\u4e2a\u4fe1\u53f7\u91cf\uff0c\u5b50\u7ebf\u7a0b\u7ed3\u675f\u540e\u8ba9\u4fe1\u53f7\u91cf+1\uff0c\u4e3b\u7ebf\u7a0b\u505an\u6b21\u4fe1\u53f7\u91cf-1\u7684\u64cd\u4f5c\uff08\u4e3a0\u7684\u65f6\u5019\u4f1a\u81ea\u52a8\u7b49\u5f85\uff09\uff0c\u7b49\u5230\u505a\u5b8cn\u6b21-1\uff0c\u8bf4\u660en\u4e2a\u5b50\u7ebf\u7a0b\u90fd\u5b8c\u6210\u4e86+1\uff0c\u8bf4\u660e\u5168\u90e8\u5b50\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u4e86\uff0c\u5c31\u53ef\u4ee5\u7ee7\u7eed\u4e86\u3002<br \/>\n\u8fd9\u53ea\u662fWaitForMultipleObjects\u7684\u4e00\u79cd\u7b80\u5355\u66ff\u4ee3\uff0c\u56e0\u4e3a\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u6240\u4ee5\u4e0d\u80fd\u5224\u65ad\u67d0\u4e2a\u7ebf\u7a0b\u51fa\u95ee\u9898\u4e0e\u5426\u3002<br \/>\n<code lang=\"c++\"><br \/>\n#include <pthread.h>\n#include <semaphore.h><br \/>\n#include <iostream><br \/>\n#include <stdio.h><br \/>\nusing namespace std;<\/p>\n<p>typedef struct pinfo<br \/>\n{<br \/>\n\tsem_t * bin_sem;<br \/>\n\tint id;<br \/>\n}pinfo;<\/p>\n<p>void *thread_function(void *arg)<br \/>\n{<br \/>\n\tpinfo *t=(pinfo*)arg;<br \/>\n\tprintf(\"thread_functiont%dt--------------sem_waitn\",t->id);<br \/>\n\tint ii=t->id + 1;<br \/>\n\tsleep(ii);    \/\/\u6682\u505c\u4e00\u4f1a\uff0c\u8fd9\u91cc\u6a21\u62df\u7a0b\u5e8f\u6267\u884c<br \/>\n\tsem_post(t->bin_sem);    \/\/\u4fe1\u53f7\u91cf+1<br \/>\n\tprintf(\"sem_donet%dn\",t->id);<br \/>\n\treturn NULL;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n\tint res;<br \/>\n\tint n=10;    \/\/\u8bbe\u7f6e\u4e2a\u7ebf\u7a0b<br \/>\n\tsem_t  bin_sem;    \/\/\u8bbe\u7f6e\u4fe1\u53f7\u91cf\uff0c\u4e0b\u9762\u521d\u59cb\u5316\u8fd9\u4e2a\u4fe1\u53f7\u91cf<\/p>\n<p>\tres = sem_init(&bin_sem, 0, 0);<\/p>\n<p>\tif (res != 0)<br \/>\n\t{<br \/>\n\t\tperror(\"Semaphore initialization failed\");<br \/>\n\t}<br \/>\n\tprintf(\"sem_initn\");<br \/>\n\tint i;<br \/>\n\tpinfo *p=new pinfo[n];    \/\/\u521b\u5efa\u4e2a\u7ebf\u7a0b\u7684\u53c2\u6570\u7ed3\u6784\u4f53<br \/>\n\tpthread_t *a_thread=new pthread_t[n];<br \/>\n\tfor(i=0;i<n;i++)\n\t{\n\t\tprintf(\"entering %dn\",i);\n\t\tp[i].bin_sem=&#038;bin_sem;    \/\/\u8ba9\u53c2\u6570\u4e2d\u95f4\u7684\u4fe1\u53f7\u91cf\u6307\u5411main\u4e2d\u7684\u4fe1\u53f7\u91cf\uff08\u5171\u7528\u4e00\u4e2a\u4fe1\u53f7\u91cf\uff09\n\t\tp[i].id=i+1;\n\t\tprintf(\"init OK %dn\",i);\n\t\tres = pthread_create(&#038;a_thread[i], NULL, thread_function, &#038;p[i]);    \/\/\u521b\u5efa\u7ebf\u7a0b\n\t\tif (res != 0)\n\t\t{\n\t\t\tprintf(\"Thread creation failure\");\n\t\t}\n\t}\n\tprintf(\"thread_function_mainn\");\n\tfor(i=0;i<n;i++)\n\t{\n\t\tsem_wait(&#038;bin_sem);    \/\/\u5faa\u73af\u7b49\u5f85\u6b21\uff0c\u76f8\u5f53\u4e8e\u4e2a\u7ebf\u7a0b\u90fd\u6267\u884c\u5b8c\u4e86\n\t}\n\tprintf(\"thread_main_exitn\");\n\tsem_destroy(&#038;bin_sem);        \/\/\u91ca\u653e\u4fe1\u53f7\u91cf\n\treturn 0;\n}\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u501f\u52a9\u4e86linux\u4e0b\u7684Semaphore\uff08\u4fe1\u53f7\u91cf\uff09\uff0c\u5b9e\u73b0\u4e86\u4e00\u79cd\u7b80\u5355\u7684WaitForMultipleObjects &hellip; <a href=\"https:\/\/blog.hymike.net\/?p=138\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u89e3\u51b3linux\u4e0bWaitForMultipleObjects\u7684\u4e00\u79cd\u65b9\u6cd5\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.hymike.net\/index.php?rest_route=\/wp\/v2\/posts\/138"}],"collection":[{"href":"https:\/\/blog.hymike.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hymike.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hymike.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hymike.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138"}],"version-history":[{"count":0,"href":"https:\/\/blog.hymike.net\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.hymike.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hymike.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hymike.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}