{"id":175195,"date":"2024-11-08T03:50:28","date_gmt":"2024-11-07T19:50:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/175195\/"},"modified":"2024-11-08T03:50:28","modified_gmt":"2024-11-07T19:50:28","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e7%b7%9a%e7%a8%8b%e6%b1%a0%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-c-%e7%b7%9a%e7%a8%8b%e6%b1%a0-%e6%93%8d%e4%bd%9c%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/175195\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u7dda\u7a0b\u6c60\u64cd\u4f5c\u6578\u64da\u5eab\uff1f (c \u7dda\u7a0b\u6c60 \u64cd\u4f5c\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8c%e8%aa%9e%e8%a8%80%e7%b7%9a%e7%a8%8b%e6%b1%a0%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-HCXLDApzzQ\">\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u7dda\u7a0b\u6c60\u64cd\u4f5c\u6578\u64da\u5eab\uff1f<\/h1>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6578\u64da\u5eab\u64cd\u4f5c\u5f80\u5f80\u662f\u6027\u80fd\u74f6\u9838\u7684\u4e3b\u8981\u4f86\u6e90\u3002\u70ba\u4e86\u63d0\u9ad8\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u4f7f\u7528\u7dda\u7a0b\u6c60\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u5be6\u73fe\u7dda\u7a0b\u6c60\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%b7%9a%e7%a8%8b%e6%b1%a0%ef%bc%9f-HCXLDApzzQ\">\u4ec0\u9ebc\u662f\u7dda\u7a0b\u6c60\uff1f<\/h2>\n<p>\u7dda\u7a0b\u6c60\u662f\u4e00\u7a2e\u7ba1\u7406\u7dda\u7a0b\u7684\u6280\u8853\uff0c\u5b83\u9810\u5148\u5275\u5efa\u4e00\u5b9a\u6578\u91cf\u7684\u7dda\u7a0b\uff0c\u4e26\u5c07\u5176\u653e\u5165\u6c60\u4e2d\u4ee5\u4f9b\u91cd\u8907\u4f7f\u7528\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u983b\u7e41\u5275\u5efa\u548c\u92b7\u6bc0\u7dda\u7a0b\u6240\u5e36\u4f86\u7684\u958b\u92b7\uff0c\u5f9e\u800c\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u7dda\u7a0b\u6c60\u901a\u5e38\u7528\u65bc\u9700\u8981\u8655\u7406\u5927\u91cf\u77ed\u671f\u4efb\u52d9\u7684\u5834\u666f\uff0c\u4f8b\u5982\u6578\u64da\u5eab\u67e5\u8a62\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8%e7%b7%9a%e7%a8%8b%e6%b1%a0%e6%93%8d%e4%bd%9c%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-HCXLDApzzQ\">\u70ba\u4ec0\u9ebc\u4f7f\u7528\u7dda\u7a0b\u6c60\u64cd\u4f5c\u6578\u64da\u5eab\uff1f<\/h2>\n<ul>\n<li><strong>\u63d0\u9ad8\u6027\u80fd\uff1a<\/strong>\u901a\u904e\u91cd\u7528\u7dda\u7a0b\uff0c\u6e1b\u5c11\u4e86\u5275\u5efa\u548c\u92b7\u6bc0\u7dda\u7a0b\u7684\u958b\u92b7\u3002<\/li>\n<li><strong>\u8cc7\u6e90\u7ba1\u7406\uff1a<\/strong>\u7dda\u7a0b\u6c60\u53ef\u4ee5\u9650\u5236\u540c\u6642\u57f7\u884c\u7684\u7dda\u7a0b\u6578\u91cf\uff0c\u907f\u514d\u904e\u591a\u7684\u7dda\u7a0b\u5c0e\u81f4\u7cfb\u7d71\u8cc7\u6e90\u8017\u76e1\u3002<\/li>\n<li><strong>\u7c21\u5316\u4ee3\u78bc\uff1a<\/strong>\u4f7f\u7528\u7dda\u7a0b\u6c60\u53ef\u4ee5\u4f7f\u4ee3\u78bc\u7d50\u69cb\u66f4\u52a0\u6e05\u6670\uff0c\u6613\u65bc\u7dad\u8b77\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b7%9a%e7%a8%8b%e6%b1%a0%e7%9a%84%e5%9f%ba%e6%9c%ac%e7%b5%90%e6%a7%8b-HCXLDApzzQ\">\u7dda\u7a0b\u6c60\u7684\u57fa\u672c\u7d50\u69cb<\/h2>\n<p>\u5728C\u8a9e\u8a00\u4e2d\u5be6\u73fe\u7dda\u7a0b\u6c60\u901a\u5e38\u9700\u8981\u4f7f\u7528POSIX\u7dda\u7a0b\uff08pthread\uff09\u5eab\u3002\u4ee5\u4e0b\u662f\u7dda\u7a0b\u6c60\u7684\u57fa\u672c\u7d50\u69cb\uff1a<\/p>\n<pre><code>\n#include &lt;pthread.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n\n#define MAX_THREADS 5\n#define QUEUE_SIZE 10\n\ntypedef struct {\n    void (*function)(void*);\n    void *argument;\n} task_t;\n\ntypedef struct {\n    pthread_mutex_t lock;\n    pthread_cond_t notify;\n    task_t queue[QUEUE_SIZE];\n    int queue_size;\n    int thread_count;\n    int shutdown;\n} threadpool_t;\n<\/code><\/pre>\n<h2 id=\"%e7%b7%9a%e7%a8%8b%e6%b1%a0%e7%9a%84%e5%88%9d%e5%a7%8b%e5%8c%96-HCXLDApzzQ\">\u7dda\u7a0b\u6c60\u7684\u521d\u59cb\u5316<\/h2>\n<p>\u5728\u521d\u59cb\u5316\u7dda\u7a0b\u6c60\u6642\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u6307\u5b9a\u6578\u91cf\u7684\u7dda\u7a0b\u4e26\u5c07\u5176\u653e\u5165\u6c60\u4e2d\u3002\u4ee5\u4e0b\u662f\u521d\u59cb\u5316\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nthreadpool_t* threadpool_create(int thread_count) {\n    threadpool_t *pool = malloc(sizeof(threadpool_t));\n    pool-&gt;thread_count = thread_count;\n    pool-&gt;queue_size = 0;\n    pool-&gt;shutdown = 0;\n    pthread_mutex_init(&amp;pool-&gt;lock, NULL);\n    pthread_cond_init(&amp;pool-&gt;notify, NULL);\n    \n    for (int i = 0; i &lt; thread_count; i++) {\n        pthread_create(&amp;threads[i], NULL, thread_function, (void*)pool);\n    }\n    return pool;\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b7%9a%e7%a8%8b%e5%9f%b7%e8%a1%8c%e4%bb%bb%e5%8b%99-HCXLDApzzQ\">\u7dda\u7a0b\u57f7\u884c\u4efb\u52d9<\/h2>\n<p>\u6bcf\u500b\u7dda\u7a0b\u5728\u555f\u52d5\u5f8c\u6703\u9032\u5165\u4e00\u500b\u5faa\u74b0\uff0c\u7b49\u5f85\u4efb\u52d9\u7684\u5230\u4f86\u3002\u7576\u6709\u4efb\u52d9\u6642\uff0c\u7dda\u7a0b\u6703\u5f9e\u4efb\u52d9\u968a\u5217\u4e2d\u53d6\u51fa\u4e26\u57f7\u884c\u3002\u4ee5\u4e0b\u662f\u7dda\u7a0b\u57f7\u884c\u4efb\u52d9\u7684\u4ee3\u78bc\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nvoid* thread_function(void *arg) {\n    threadpool_t *pool = (threadpool_t*)arg;\n    while (1) {\n        pthread_mutex_lock(&amp;pool-&gt;lock);\n        while (pool-&gt;queue_size == 0 &amp;&amp; !pool-&gt;shutdown) {\n            pthread_cond_wait(&amp;pool-&gt;notify, &amp;pool-&gt;lock);\n        }\n        if (pool-&gt;shutdown) {\n            break;\n        }\n        task_t task = pool-&gt;queue[--pool-&gt;queue_size];\n        pthread_mutex_unlock(&amp;pool-&gt;lock);\n        \n        \/\/ \u57f7\u884c\u6578\u64da\u5eab\u64cd\u4f5c\n        task.function(task.argument);\n    }\n    pthread_mutex_unlock(&amp;pool-&gt;lock);\n    return NULL;\n}\n<\/code><\/pre>\n<h2 id=\"%e5%b0%87%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e8%88%87%e7%b7%9a%e7%a8%8b%e6%b1%a0%e7%b5%90%e5%90%88-HCXLDApzzQ\">\u5c07\u6578\u64da\u5eab\u64cd\u4f5c\u8207\u7dda\u7a0b\u6c60\u7d50\u5408<\/h2>\n<p>\u5728\u4f7f\u7528\u7dda\u7a0b\u6c60\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u5c07\u6578\u64da\u5eab\u67e5\u8a62\u5c01\u88dd\u6210\u4efb\u52d9\uff0c\u7136\u5f8c\u5c07\u5176\u6dfb\u52a0\u5230\u7dda\u7a0b\u6c60\u7684\u4efb\u52d9\u968a\u5217\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u6578\u64da\u5eab\u67e5\u8a62\u4efb\u52d9\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nvoid db_query(void *arg) {\n    \/\/ \u5047\u8a2d arg \u662f\u4e00\u500b\u5305\u542b\u67e5\u8a62\u4fe1\u606f\u7684\u7d50\u69cb\n    \/\/ \u9019\u88e1\u57f7\u884c\u6578\u64da\u5eab\u67e5\u8a62\n    printf(\"\u57f7\u884c\u6578\u64da\u5eab\u67e5\u8a62...n\");\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HCXLDApzzQ\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528C\u8a9e\u8a00\u5be6\u73fe\u7dda\u7a0b\u6c60\u4f86\u64cd\u4f5c\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u6709\u6548\u7684\u63d0\u9ad8\u6027\u80fd\u7684\u65b9\u5f0f\u3002\u901a\u904e\u5408\u7406\u7ba1\u7406\u7dda\u7a0b\u548c\u4efb\u52d9\uff0c\u6211\u5011\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u5ef6\u9072\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u8cc7\u6e90\u7684\u5229\u7528\u7387\uff0c\u9084\u7c21\u5316\u4e86\u4ee3\u78bc\u7d50\u69cb\uff0c\u4fbf\u65bc\u7dad\u8b77\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u670d\u52d9\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528C\u8a9e\u8a00\u7684\u7dda\u7a0b\u6c60\u4f86\u9ad8\u6548\u64cd\u4f5c\u6578\u64da\u5eab\uff0c\u63d0\u5347\u6027\u80fd\u4e26\u7c21\u5316\u591a\u7dda\u7a0b\u7ba1\u7406\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-175195","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=175195"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175195\/revisions"}],"predecessor-version":[{"id":175196,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175195\/revisions\/175196"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=175195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=175195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=175195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}