{"id":2184,"date":"2025-07-21T18:17:37","date_gmt":"2025-07-21T10:17:37","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2184"},"modified":"2025-07-21T18:22:49","modified_gmt":"2025-07-21T10:22:49","slug":"%e4%bf%a1%e5%8f%b7%e9%87%8fdemo","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2184","title":{"rendered":"\u4fe1\u53f7\u91cfdemo"},"content":{"rendered":"\n<p><strong>\u6761\u4ef6\u53d8\u91cf\uff08Condition Variable\uff09\u548c\u4fe1\u53f7\u91cf\uff08Semaphore\uff09\u662f\u4e24\u79cd\u4e0d\u540c\u7684\u540c\u6b65\u539f\u8bed<\/strong>\uff0c\u5c3d\u7ba1\u5b83\u4eec\u90fd\u7528\u4e8e\u7ebf\u7a0b\u95f4\u534f\u8c03\uff0c\u4f46\u8bbe\u8ba1\u76ee\u6807\u3001\u4f7f\u7528\u65b9\u5f0f\u548c\u9002\u7528\u573a\u666f\u6709\u672c\u8d28\u533a\u522b\u3002\u4ee5\u4e0b\u662f\u5173\u952e\u5bf9\u6bd4\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading is-style-default\"><strong>\u6838\u5fc3\u533a\u522b\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u7279\u6027<\/strong><\/th><th><strong>\u6761\u4ef6\u53d8\u91cf (Condition Variable)<\/strong><\/th><th><strong>\u4fe1\u53f7\u91cf (Semaphore)<\/strong><\/th><\/tr><\/thead><tbody><tr><td><strong>\u672c\u8d28<\/strong><\/td><td><strong>\u65e0\u72b6\u6001<\/strong>\uff0c\u4f9d\u8d56\u5916\u90e8\u6761\u4ef6\u5224\u65ad<\/td><td><strong>\u6709\u72b6\u6001<\/strong>\uff08\u6574\u6570\u8ba1\u6570\u5668\uff09<\/td><\/tr><tr><td><strong>\u64cd\u4f5c\u5bf9\u8c61<\/strong><\/td><td>\u5fc5\u987b\u7ed1\u5b9a\u4e00\u4e2a\u4e92\u65a5\u9501\uff08Mutex\uff09\u4f7f\u7528<\/td><td>\u72ec\u7acb\u64cd\u4f5c<\/td><\/tr><tr><td><strong>\u5524\u9192\u673a\u5236<\/strong><\/td><td>\u9700\u624b\u52a8\u901a\u77e5\uff08<code>notify<\/code>\uff09<\/td><td>\u81ea\u52a8\u91ca\u653e\uff08<code>post<\/code> \u589e\u52a0\u8ba1\u6570\uff09<\/td><\/tr><tr><td><strong>\u7b49\u5f85\u884c\u4e3a<\/strong><\/td><td><code>wait()<\/code> \u4f1a<strong>\u91ca\u653e\u9501\u5e76\u963b\u585e<\/strong><\/td><td><code>wait()<\/code>\uff08\u6216 <code>P()<\/code>\uff09<strong>\u963b\u585e<\/strong><\/td><\/tr><tr><td><strong>\u5178\u578b\u7528\u9014<\/strong><\/td><td>\u7b49\u5f85\u7279\u5b9a\u6761\u4ef6\u6210\u7acb\uff08\u5982\u201c\u961f\u5217\u975e\u7a7a\u201d\uff09<\/td><td>\u63a7\u5236\u8d44\u6e90\u8bbf\u95ee\u6570\u91cf\uff08\u5982\u9650\u6d41\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u5173\u4e8e\u6761\u4ef6\u53d8\u91cf\u7684\u4f7f\u7528\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=672\">https:\/\/www.madbull.site\/?p=672<\/a>\uff0c\u5728 pthread_pool \u9879\u76ee\u4e2d\uff0c\u4e3b\u8981\u662f\u4f7f\u7528 \u6761\u4ef6\u53d8\u91cf \u5b9e\u73b0\u4efb\u52a1\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u4ece\u8fd9\u7bc7\u6587\u7ae0\u4e0b\u8f7d\u6e90\u7801\u67e5\u770b\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--30);margin-bottom:var(--wp--preset--spacing--30);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading is-style-default\"><strong>\u4eca\u5929\u8bb0\u5f55\u4e00\u4e0b\u4fe1\u53f7\u91cf\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ gcc sem.c -lpthread -o sem\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;pthread.h&gt;\n#include &lt;semaphore.h&gt;\n#include &lt;unistd.h&gt;\n\n#define BUFFER_SIZE 5  \/\/ \u7f13\u51b2\u533a\u5927\u5c0f\n#define NUM_ITEMS 10   \/\/ \u6bcf\u4e2a\u751f\u4ea7\u8005\u751f\u4ea7\u7684\u9879\u76ee\u6570\u91cf\n\n\/\/ \u5171\u4eab\u7f13\u51b2\u533a\nint buffer&#91;BUFFER_SIZE];\nint in = 0;  \/\/ \u751f\u4ea7\u8005\u63d2\u5165\u4f4d\u7f6e\nint out = 0; \/\/ \u6d88\u8d39\u8005\u53d6\u51fa\u4f4d\u7f6e\n\n\/\/ \u4fe1\u53f7\u91cf\nsem_t empty;   \/\/ \u7a7a\u69fd\u4f4d\u4fe1\u53f7\u91cf\nsem_t full;    \/\/ \u6ee1\u69fd\u4f4d\u4fe1\u53f7\u91cf\nsem_t mutex;   \/\/ \u4e92\u65a5\u4fe1\u53f7\u91cf\uff08\u4fdd\u62a4\u7f13\u51b2\u533a\uff09\n\n\/\/ \u751f\u4ea7\u8005\u7ebf\u7a0b\u51fd\u6570\nvoid* producer(void* arg) {\n    int producer_id = *((int*)arg);\n    \n    for (int i = 0; i &lt; NUM_ITEMS; i++) {\n        \/\/ \u751f\u4ea7\u4e00\u4e2a\u9879\u76ee\uff08\u6a21\u62df\u8017\u65f6\uff09\n        int item = rand() % 100;\n        \n        sem_wait(&amp;empty);  \/\/ \u7b49\u5f85\u7a7a\u69fd\u4f4d\uff08P\u64cd\u4f5c\uff09\n        sem_wait(&amp;mutex);  \/\/ \u8fdb\u5165\u4e34\u754c\u533a\uff08\u4fdd\u62a4\u7f13\u51b2\u533a\uff09\n        \n        \/\/ \u5c06\u9879\u76ee\u653e\u5165\u7f13\u51b2\u533a\n        buffer&#91;in] = item;\n        printf(\"\u751f\u4ea7\u8005 %d \u751f\u4ea7: %d (\u4f4d\u7f6e: %d)\\n\", producer_id, item, in);\n        in = (in + 1) % BUFFER_SIZE;\n        \n        sem_post(&amp;mutex);  \/\/ \u79bb\u5f00\u4e34\u754c\u533a\n        sem_post(&amp;full);   \/\/ \u589e\u52a0\u6ee1\u69fd\u4f4d\uff08V\u64cd\u4f5c\uff09\n        \n        usleep(rand() % 100000); \/\/ \u968f\u673a\u7761\u7720\n    }\n    \n    return NULL;\n}\n\n\/\/ \u6d88\u8d39\u8005\u7ebf\u7a0b\u51fd\u6570\nvoid* consumer(void* arg) {\n    int consumer_id = *((int*)arg);\n    \n    for (int i = 0; i &lt; NUM_ITEMS; i++) {\n        sem_wait(&amp;full);   \/\/ \u7b49\u5f85\u6ee1\u69fd\u4f4d\uff08P\u64cd\u4f5c\uff09\n        sem_wait(&amp;mutex);  \/\/ \u8fdb\u5165\u4e34\u754c\u533a\uff08\u4fdd\u62a4\u7f13\u51b2\u533a\uff09\n        \n        \/\/ \u4ece\u7f13\u51b2\u533a\u53d6\u51fa\u9879\u76ee\n        int item = buffer&#91;out];\n        printf(\"\u6d88\u8d39\u8005 %d \u6d88\u8d39: %d (\u4f4d\u7f6e: %d)\\n\", consumer_id, item, out);\n        out = (out + 1) % BUFFER_SIZE;\n        \n        sem_post(&amp;mutex);  \/\/ \u79bb\u5f00\u4e34\u754c\u533a\n        sem_post(&amp;empty);  \/\/ \u589e\u52a0\u7a7a\u69fd\u4f4d\uff08V\u64cd\u4f5c\uff09\n        \n        \/\/ \u6d88\u8d39\u9879\u76ee\uff08\u6a21\u62df\u8017\u65f6\uff09\n        usleep(rand() % 150000); \/\/ \u968f\u673a\u7761\u7720\n    }\n    \n    return NULL;\n}\n\nint main() {\n    pthread_t prod_thread1, prod_thread2;\n    pthread_t cons_thread1, cons_thread2;\n    \n    int prod_id1 = 1, prod_id2 = 2;\n    int cons_id1 = 1, cons_id2 = 2;\n    \n    \/\/ \u521d\u59cb\u5316\u4fe1\u53f7\u91cf\n    sem_init(&amp;empty, 0, BUFFER_SIZE); \/\/ \u521d\u59cb\u7a7a\u69fd\u4f4d\u6570 = \u7f13\u51b2\u533a\u5927\u5c0f\n    sem_init(&amp;full, 0, 0);           \/\/ \u521d\u59cb\u6ee1\u69fd\u4f4d\u6570 = 0\n    sem_init(&amp;mutex, 0, 1);           \/\/ \u4e92\u65a5\u4fe1\u53f7\u91cf\u521d\u59cb\u4e3a1\uff08\u4e8c\u8fdb\u5236\u4fe1\u53f7\u91cf\uff09\n    \n    \/\/ \u521b\u5efa\u751f\u4ea7\u8005\u7ebf\u7a0b\n    pthread_create(&amp;prod_thread1, NULL, producer, &amp;prod_id1);\n    pthread_create(&amp;prod_thread2, NULL, producer, &amp;prod_id2);\n    \n    \/\/ \u521b\u5efa\u6d88\u8d39\u8005\u7ebf\u7a0b\n    pthread_create(&amp;cons_thread1, NULL, consumer, &amp;cons_id1);\n    pthread_create(&amp;cons_thread2, NULL, consumer, &amp;cons_id2);\n    \n    \/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\n    pthread_join(prod_thread1, NULL);\n    pthread_join(prod_thread2, NULL);\n    pthread_join(cons_thread1, NULL);\n    pthread_join(cons_thread2, NULL);\n    \n    \/\/ \u9500\u6bc1\u4fe1\u53f7\u91cf\n    sem_destroy(&amp;empty);\n    sem_destroy(&amp;full);\n    sem_destroy(&amp;mutex);\n    \n    printf(\"\u6240\u6709\u751f\u4ea7\u548c\u6d88\u8d39\u5b8c\u6210!\\n\");\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading is-style-default\"><strong>\u7f16\u8bd1\u6d4b\u8bd5\uff1a<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"516\" height=\"256\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2025\/07\/111.png\" alt=\"\" class=\"wp-image-2185\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u4fe1\u53f7\u91cf\u5c0f\u4f8b\u5b50<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-with-sidebar","format":"standard","meta":{"footnotes":""},"categories":[156,154],"tags":[175,673],"class_list":["post-2184","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-c","tag-673"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2184"}],"version-history":[{"count":2,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2184\/revisions"}],"predecessor-version":[{"id":2187,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2184\/revisions\/2187"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}