{"id":54,"date":"2019-03-17T17:19:00","date_gmt":"2019-03-17T09:19:00","guid":{"rendered":"http:\/\/175.178.247.106\/?p=54"},"modified":"2025-03-05T17:21:51","modified_gmt":"2025-03-05T09:21:51","slug":"c%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"http:\/\/175.178.247.106\/index.php\/2019\/03\/17\/c%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ba%bf%e7%a8%8b%e6%b1%a0\/","title":{"rendered":"C\u8bed\u8a00\u5b9e\u73b0\u4e00\u4e2a\u7ebf\u7a0b\u6c60"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p id=\"u465f05f0\">\u5148\u5b66\u4e60\u51e0\u4e2a pthread \u7684\u7ebf\u7a0b\u51fd\u6570\uff1a<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hzfk3\"><code>pthread_cond_wait<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"69fa3701\">\u4f5c\u7528<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8ba9\u7ebf\u7a0b\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\u3002<\/li>\n\n\n\n<li>\u5728\u7b49\u5f85\u671f\u95f4\uff0c\u7ebf\u7a0b\u4f1a\u91ca\u653e\u5173\u8054\u7684\u4e92\u65a5\u9501\uff0c\u4ee5\u907f\u514d\u6b7b\u9501\u3002<\/li>\n\n\n\n<li>\u5f53\u6761\u4ef6\u6ee1\u8db3\u65f6\uff0c\u7ebf\u7a0b\u4f1a\u88ab\u5524\u9192\u5e76\u91cd\u65b0\u83b7\u53d6\u4e92\u65a5\u9501\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"YmfOK\"><code>pthread_cond_signal<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"Y5Pv2\">\u4f5c\u7528<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5524\u9192 <strong>\u4e00\u4e2a<\/strong> \u6b63\u5728\u7b49\u5f85\u6761\u4ef6\u53d8\u91cf\u7684\u7ebf\u7a0b\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6709\u591a\u4e2a\u7ebf\u7a0b\u5728\u7b49\u5f85\uff0c\u5177\u4f53\u5524\u9192\u54ea\u4e00\u4e2a\u7ebf\u7a0b\u662f\u4e0d\u786e\u5b9a\u7684\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sBOzY\"><code>pthread_cond_broadcast<\/code><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"kxxSY\">\u4f5c\u7528<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5524\u9192 <strong>\u6240\u6709<\/strong> \u6b63\u5728\u7b49\u5f85\u6761\u4ef6\u53d8\u91cf\u7684\u7ebf\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<p id=\"u1441809e\">\u4e0a\u8ff0\u4e09\u4e2a\u51fd\u6570\uff0c\u7ecf\u5e38\u662f\u914d\u5408\u662f\u914d\u5408\u4f7f\u7528\u3002<\/p>\n\n\n\n<p id=\"ud541e67d\">\u57fa\u672c\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6709\u4e00\u4e2a\u7ebf\u7a0b\u961f\u5217<\/li>\n\n\n\n<li>\u6709\u4efb\u52a1\u9700\u8981\u6267\u884c\u65f6\uff0c\u4ece\u7ebf\u7a0b\u961f\u5217\u4e2d\u53d6\u51fa\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1<\/li>\n\n\n\n<li>\u4efb\u52a1\u6267\u884c\u5b8c\u518d\u6b21\u8fdb\u5165 pending \u72b6\u6001\uff0c\u7b49\u5f85\u5524\u9192<\/li>\n<\/ul>\n\n\n\n<p id=\"u3c56ea3a\">\u5173\u952e\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p id=\"u30021426\">\u4efb\u52a1\u4ee5\u53ca\u7ebf\u7a0b\u6c60\u7ed3\u6784\u4f53<\/p>\n\n\n\n<pre id=\"GpRIA\" class=\"wp-block-code\"><code>\/\/ \u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u53c2\u6570\ntypedef struct\n{\n    void (*func)(void *args); \/\/ \u4efb\u52a1\u51fd\u6570\u6307\u9488\n    void *args\n    } Task;\n\n\/\/ \u7ebf\u7a0b\u6c60\u53c2\u6570\ntypedef struct\n{\n    pthread_mutex_t lock; \/\/ \u7ebf\u7a0b\u6c60\u4e92\u65a5\u9501\n    pthread_cond_t cond;  \/\/ \u7ebf\u7a0b\u6c60\u540c\u6b65\u4fe1\u53f7\n\n    pthread_t *threads;      \/\/ \u4fdd\u5b58\u7ebf\u7a0b\u6c60\u521b\u5efa\u7684\u6240\u6709\u7ebf\u7a0b\n    int32_t threadMaxNum;    \/\/ \u6700\u5927\u53ef\u521b\u5efa\u7ebf\u7a0b\u6570\n    int32_t threadStartStep; \/\/ \u521d\u59cb\u542f\u52a8\u7ebf\u7a0b\u7684\u4e2a\u6570\n    int32_t threadStartCnt;  \/\/ \u5df2\u542f\u52a8\u7ebf\u7a0b\u4e2a\u6570\n    int32_t threadPendCnt;   \/\/ \u5df2\u542f\u52a8\u4f46\u662f\u5904\u4e8ePending\u72b6\u6001\u7684\u7ebf\u7a0b\n\n    Task *taskQueue; \/\/ \u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u961f\u5217\n    int32_t taskQueueSize;     \/\/ \u4efb\u52a1\u961f\u5217\u7684\u5927\u5c0f\n    int32_t taskQueueHead;     \/\/ \u5f53\u524d\u4efb\u52a1\u961f\u5217\u5934\u7d22\u5f15\n    int32_t taskQueueTail;     \/\/ \u5f53\u524d\u4efb\u52a1\u961f\u5217\u5c3e\u7d22\u5f15\n    int32_t taskPendCnt;       \/\/ \u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u4e2a\u6570\n\n    int32_t isShutdown; \/\/ \u7ebf\u7a0b\u6c60\u6b63\u5728\u5173\u95ed\n} ThreadpoolInfo;<\/code><\/pre>\n\n\n\n<p id=\"u8b949524\">\u7ebf\u7a0b\u6c60\u7684\u521b\u5efa\uff1a<\/p>\n\n\n\n<pre id=\"mr3QK\" class=\"wp-block-code\"><code>ThreadpoolInfo *threadpool_create(\n    int32_t threadMaxNum,\n    int32_t threadStartStep,\n    int32_t taskQueueSize)\n{\n    ThreadpoolInfo *threadpool = NULL;\n\n    if ((0 &gt;= threadMaxNum) || (0 &gt;= threadStartStep) || (0 &gt;= taskQueueSize))\n    {\n        THREADPOOL_ERR(\"invalid param.\\r\\n\");\n        goto error_exit;\n    }\n\n    threadpool = (ThreadpoolInfo *)malloc(sizeof(ThreadpoolInfo));\n    if (NULL == threadpool)\n    {\n        THREADPOOL_ERR(\"malloc threadpool failed.\\r\\n\");\n        goto error_exit;\n    }\n\n    memset(threadpool, 0, sizeof(ThreadpoolInfo));\n    threadpool-&gt;threadMaxNum = threadMaxNum;\n    threadpool-&gt;threadStartStep = threadStartStep;\n    threadpool-&gt;taskQueueSize = taskQueueSize;\n\n    \/\/ \u5206\u914d\u7ebf\u7a0b\u5b58\u50a8\u8d44\u6e90 *\/\n    threadpool-&gt;threads = (pthread_t *)calloc(threadMaxNum, sizeof(pthread_t));\n    if (NULL == threadpool-&gt;threads)\n    {\n        THREADPOOL_ERR(\"malloc threads failed.\\r\\n\");\n        goto error_exit;\n    }\n\n    \/\/ \u5206\u914d\u4efb\u52a1\u961f\u5217 *\/\n    threadpool-&gt;taskQueue = (Task *)calloc(taskQueueSize, sizeof(Task));\n    if (NULL == threadpool-&gt;taskQueue)\n    {\n        THREADPOOL_ERR(\"malloc task queue failed.\\r\\n\");\n        goto error_exit;\n    }\n\n    \/\/ \u521d\u59cb\u5316\u4e92\u65a5\u4fe1\u53f7\u91cf\u548c\u540c\u6b65\u4fe1\u53f7 *\/\n    if (0 != THREADPOOL_LOCK_INIT(threadpool))\n    {\n        THREADPOOL_ERR(\"mutex init failed.\\r\\n\");\n        goto error_exit;\n    }\n\n    if (0 != THREADPOOL_COND_INIT(threadpool))\n    {\n        THREADPOOL_ERR(\"cond init failed.\\r\\n\");\n        goto error_exit;\n    }\n\n    return threadpool;\n\nerror_exit:\n\n    if (threadpool != NULL)\n    {\n        threadpool_free(threadpool);\n    }\n\n    return NULL;\n}<\/code><\/pre>\n\n\n\n<p id=\"u3fd1647d\">\u589e\u52a0\u4efb\u52a1\uff1a<\/p>\n\n\n\n<pre id=\"IQts3\" class=\"wp-block-code\"><code>int32_t threadpool_addtask(\n    ThreadpoolInfo *threadpool,\n    THREADPOOLTASKFUNC taskfunc,\n    void *args)\n{\n    int32_t ret = 0;\n\n    if ((NULL == threadpool) || (NULL == taskfunc))\n    {\n        THREADPOOL_ERR(\"invalid param.\\r\\n\");\n        return -1;\n    }\n\n    THREADPOOL_LOCK(threadpool);\n\n    do\n    {\n        if (threadpool-&gt;isShutdown)\n        {\n            THREADPOOL_ERR(\"threadpool is shutdown.\\r\\n\");\n            ret = -1;\n            break;\n        }\n\n        \/\/ \u5224\u65ad\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u961f\u5217\u662f\u5426\u6ee1 *\/\n        if (threadpool-&gt;taskPendCnt == threadpool-&gt;taskQueueSize)\n        {\n            THREADPOOL_ERR(\"task queue is full.\\r\\n\");\n            ret = -1;\n            break;\n        }\n\n        \/\/ \u5982\u679cpending\u72b6\u6001\u7684\u7ebf\u7a0b\u5df2\u7528\u5b8c\uff0c\u5219\u542f\u52a8\u65b0\u7684\u7ebf\u7a0b *\/\n        if (threadpool-&gt;threadPendCnt &lt;= 0)\n        {\n            if (0 != threadpool_start(threadpool))\n            {\n                ret = -1;\n                break;\n            }\n        }\n\n        \/\/ \u5c06\u4efb\u52a1\u653e\u5165\u5bf9\u5c3e *\/\n        threadpool-&gt;taskQueue&#91;threadpool-&gt;taskQueueTail].func = taskfunc;\n        threadpool-&gt;taskQueue&#91;threadpool-&gt;taskQueueTail].args = args;\n\n        threadpool-&gt;taskQueueTail = (threadpool-&gt;taskQueueTail + 1) % threadpool-&gt;taskQueueSize;\n        threadpool-&gt;taskPendCnt++;\n\n        \/\/ \u5524\u9192\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4efb\u52a1 *\/\n        THREADPOOL_COND_SIGNAL(threadpool);\n\n    } while (0);\n\n    THREADPOOL_UNLOCK(threadpool);\n    return ret;\n}<\/code><\/pre>\n\n\n\n<p id=\"u607f0a09\">\u7ebf\u7a0b\u56de\u8c03\uff1a<\/p>\n\n\n\n<pre id=\"AZopR\" class=\"wp-block-code\"><code>void *thread_callback(void *arg)\n{\n    ThreadpoolInfo *threadpool = (ThreadpoolInfo *)arg;\n    Task task;\n\n    while (1)\n    {\n        THREADPOOL_LOCK(threadpool);\n\n        \/\/ \u7b49\u5f85\u4efb\u52a1\u5206\u914d\u7684\u4fe1\u53f7\n        \/\/ \u5982\u679c\u5f53\u524d\u6ca1\u6709\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\uff0c\u5e76\u4e14\u7ebf\u7a0b\u6c60\u6ca1\u6709\u5173\u95ed\u5219\u7ee7\u7eed\u7b49\u5f85\u4fe1\u53f7\n        while ((0 == threadpool-&gt;taskPendCnt) &amp;&amp; (0 == threadpool-&gt;isShutdown))\n        {\n            THREADPOOL_COND_WAIT(threadpool);\n        }\n\n        \/\/ \u5982\u679c\u7ebf\u7a0b\u6c60\u5df2\u5173\u95ed\uff0c\u5219\u9000\u51fa\u7ebf\u7a0b  *\/\n        if (threadpool-&gt;isShutdown)\n            break;\n\n        \/\/ \u53d6\u4efb\u52a1\u961f\u5217\u4e2d\u5f53\u524d\u7b2c\u4e00\u4e2a\u4efb\u52a1 *\/\n        task.func = threadpool-&gt;taskQueue&#91;threadpool-&gt;taskQueueHead].func;\n        task.args = threadpool-&gt;taskQueue&#91;threadpool-&gt;taskQueueHead].args;\n\n        threadpool-&gt;taskQueueHead = (threadpool-&gt;taskQueueHead + 1) % threadpool-&gt;taskQueueSize;\n        threadpool-&gt;taskPendCnt--;\n        threadpool-&gt;threadPendCnt--;\n\n        THREADPOOL_UNLOCK(threadpool);\n\n        \/\/ \u6267\u884c\u4efb\u52a1 *\/\n        (*(task.func))(task.args);\n\n        \/\/ \u4efb\u52a1\u6267\u884c\u5b8c\u6210\u540e\uff0c\u7ebf\u7a0b\u8fdb\u5165pending\u72b6\u6001 *\/\n        THREADPOOL_LOCK(threadpool);\n        threadpool-&gt;threadPendCnt++;\n        THREADPOOL_UNLOCK(threadpool);\n    }\n\n    threadpool-&gt;threadStartCnt--;\n    THREADPOOL_UNLOCK(threadpool);\n\n    pthread_exit(NULL);\n}<\/code><\/pre>\n\n\n\n<p id=\"uf0f1b23f\">\u6e90\u7801\u5730\u5740\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/gitee.com\/jesson-deng\/cthreadpool\">https:\/\/gitee.com\/jesson-deng\/cthreadpool<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u5b66\u4e60\u51e0\u4e2a pthread \u7684\u7ebf\u7a0b\u51fd\u6570\uff1a pthread_cond_wait \u4f5c\u7528 pthread_cond_signal \u4f5c\u7528 pthread_cond_br&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/175.178.247.106\/index.php\/2019\/03\/17\/c%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e7%ba%bf%e7%a8%8b%e6%b1%a0\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-c-c"],"_links":{"self":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":2,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":56,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/posts\/54\/revisions\/56"}],"wp:attachment":[{"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/175.178.247.106\/index.php\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}