{"id":672,"date":"2024-06-14T20:25:05","date_gmt":"2024-06-14T12:25:05","guid":{"rendered":"https:\/\/www.madbull.site\/?p=672"},"modified":"2024-07-26T20:06:04","modified_gmt":"2024-07-26T12:06:04","slug":"%e7%ba%bf%e7%a8%8b%e6%b1%a0pthread-pool","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=672","title":{"rendered":"\u7ebf\u7a0b\u6c60pthread-pool"},"content":{"rendered":"\n<div class=\"wp-block-file has-link-color wp-elements-a62f2b6ea21637796a62fad500303ed1\"><a id=\"wp-block-file--media-8e9b69bb-130d-498c-bd33-8e7a4d512538\" href=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/pthread-pool.tar.gz\">pthread-pool.tar<\/a><a href=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/pthread-pool.tar.gz\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-8e9b69bb-130d-498c-bd33-8e7a4d512538\">\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<p>\u5206\u4eab\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5e93\uff0c\u70b9\u51fb\u4e0a\u8fb9\u7684\u6309\u94ae\u4e0b\u8f7d\u3002\u6e90\u4ee3\u7801\u5728 <a href=\"https:\/\/sourceforge.net\/projects\/pthread-pool\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">https:\/\/sourceforge.net\/projects\/pthread-pool\/<\/a> \uff0c\u7f16\u8bd1\u65f6\u62a5\u9519\uff0c\u6539\u4e86\u51e0\u884c\u4ee3\u7801\u3002<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff0c\u89e3\u538b: tar pzxvf pthread-pool.tar.gz\uff0c\u8fdb\u5165\u5de5\u4f5c\u76ee\u5f55: cd pthread-pool-0.0.2<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff0c\u7f16\u8bd1\uff0c\u6267\u884c\u91cc\u8fb9\u7684\u811a\u672c\u5373\u53ef: bash autocompile.sh \u3002<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7b2c\u4e09\u6b65\uff0c\u5b89\u88c5\uff0c\u6267\u884c\u547d\u4ee4\uff1amake install\u3002\u52a8\u6001\u5e93libpthreadpool.so\u4f1a\u81ea\u52a8\u5b89\u88c5\u5230 \/usr\/local\/lib \u76ee\u5f55\uff0c\u5934\u6587\u4ef6\u5219\u5728 \/usr\/local\/include \u76ee\u5f55\u3002\u7f16\u8bd1\u7684\u65f6\u5019\u53ef\u4ee5\u6307\u5b9a \u5934\u6587\u4ef6\u548cso\u5e93\u7684\u5730\u5740\uff1b\u8fd0\u884c\u7684\u65f6\u5019\u5982\u679c\u627e\u4e0d\u5230 so \u5e93\uff0c\u53ef\u4ee5\u5728 \/etc\/ld.so.conf.d\/ \u76ee\u5f55\u4e0b\u968f\u4fbf\u52a0\u4e00\u4e2a xxx.conf\u7684\u6587\u4ef6\uff0c\u91cc\u8fb9\u5185\u5bb9\u662f \/usr\/local\/lib\/ \uff0c\u7136\u540e\u6267\u884c ldconfig\uff0c\u91cd\u65b0\u52a0\u8f7d\u52a8\u6001\u5e93\u67e5\u627e\u8def\u5f84\u3002<\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-layout-flow wp-block-group-is-layout-flow\">\n<p><strong>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5e94\u7528\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u7f16\u8bd1 gcc tptp.c -lpthreadpool -Wall\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;pthread_pool.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;string.h&gt;\n\n\/\/ \u4efb\u52a1\u53c2\u6570\ntypedef struct _arg_data_t {\n    int seed ;\n    int task_id ;\n} arg_data_t ;\n\n\/\/ \u4efb\u52a1\u8fd4\u56de\u503c\ntypedef struct _rtn_t {\n    int square ;\n} rtn_t ;\n\n\/\/ \u4efb\u52a1\u6267\u884c\u51fd\u6570\nvoid * task_routine(void * args) {\n\n    static int cnt = 0 ;\n    printf(\"============== task_routine run cnt &#91;%d] ===============\\n\", ++cnt ) ;\n\n    \/\/ \u83b7\u53d6\u53c2\u6570\n    arg_data_t * arg_data = (arg_data_t *)args ;\n\n    \/\/ \u8fd0\u7b97\uff1a\u8fd4\u56de\u7684\u7ed3\u679c\n    rtn_t * rtn = (rtn_t * )calloc(1, sizeof(rtn_t)) ;\n    rtn-&gt;square = arg_data-&gt;seed * arg_data-&gt;seed ;\n\n    \/\/ \u7b49\u5f85\u4e00\u4e0b\uff0c\u770b\u4e00\u4e0b\u8f93\u51fa\u987a\u5e8f\n    sleep(2) ;\n\n    return rtn;\n}\n\n\n#define WORKERNUM 3\n#define TASKNUM 7\n\n\nint main() {\n\n    int i = 0 ;\n    int ret = 0 ;\n\n    \/\/ 1\u3001\u521d\u59cb\u5316 \u7ebf\u7a0b\u6c60 pool\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_t<\/mark><\/strong> pool ;\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_create<\/mark><\/strong>(&amp;pool, NULL) ;\n\n    \/\/ 2\u3001\u521d\u59cb\u5316 worker\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_worker_t<\/mark><\/strong> worker&#91;WORKERNUM] ;\n    memset(&amp;worker, 0x00, sizeof(worker)) ;\n    for(i=0 ; i&lt;WORKERNUM ; ++i ) {\n        <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_worker_init<\/mark><\/strong>(&amp;worker&#91;i], &amp;pool, NULL) ;\n    }\n\n    \/\/ 3\u3001\u4efb\u52a1\u53c2\u6570\u521d\u59cb\u5316\n    arg_data_t * arg_data = NULL ;\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_task_t<\/mark><\/strong> * task = NULL ;\n    for(i=0 ; i&lt;TASKNUM; ++i ) {\n        task = (<strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_task_t<\/mark><\/strong> *)calloc(1, sizeof(<strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_task_t<\/mark><\/strong>)) ;\n        arg_data = (arg_data_t *)calloc(1, sizeof(arg_data_t)) ;\n        arg_data-&gt;seed = 2+i ;\n        arg_data-&gt;task_id = i ;\n\n        \/\/ 4\u3001\u8bbe\u7f6e\u4efb\u52a1\u53c2\u6570\u548c\u8c03\u7528\u7684\u51fd\u6570\u3002\n        \/\/ \u5982\u679c\u6709worker\u7a7a\u95f2\uff0c\u5219\u5524\u9192\u4e00\u4e2aworker\u5e76\u6267\u884ctask_routine\n        ret = <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_task_init<\/mark><\/strong>(task, &amp;pool, &amp;task_routine, arg_data) ;\n        printf(\"task &#91;%d] init, ret &#91;%d]\\n\", i, ret ) ;\n    }\n\n\n    rtn_t * rtn = NULL ;\n    for(i=0 ; i&lt;TASKNUM;  ) {\n        \/\/ 5\u3001\u5c1d\u8bd5\u83b7\u53d6\u4efb\u52a1\u7ed3\u679c\n        ret = <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_tryjoin<\/mark><\/strong>(&amp;pool, &amp;task) ;\n        if(ret == 0) {            \/\/ \u83b7\u53d6\u5230\u4e00\u4e2a\u5df2\u5b8c\u6210\u7684\u4efb\u52a1\n            <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_task_getretval<\/mark><\/strong>(task, (void**)&amp;rtn) ;\n            <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_task_getarg<\/mark><\/strong>(task, (void**)&amp;arg_data) ;\n            printf(\"\\nresult :\\ttask_id &#91;%d]\\tseed &#91;%d]\\tsquare &#91;%d]\\n\", arg_data-&gt;task_id, arg_data-&gt;seed, rtn-&gt;square) ;\n\n            \/\/ 6\u3001\u91ca\u653e\u7533\u8bf7\u7684\u7a7a\u95f4\n            free(arg_data) ;\n            free(rtn) ;\n            free(task) ;\n            ++i ;\n        } else {\n            sleep(1) ;\n        }\n    }\n\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_wait<\/mark><\/strong>(&amp;pool) ;    \/\/ \u7b49\u5f85\u7ebf\u7a0b\u5168\u90e8\u5b8c\u6210   --\u7531\u4e8e\u4e0a\u4e24\u6b65\u5df2\u7ecf\u7b49\u5f85\u4e86\u4efb\u52a1\uff0c\u6b64\u5904\u5df2\u5ffd\u7565\u3002\u5982\u679c\u662f\u5f02\u6b65\u83b7\u53d6\u7ed3\u679c\uff0c\u5219\u9700\u8981\u7b49\u5f85\u4efb\u52a1\u5b8c\u6210\u3002\n\n    \/\/ 7\u3001\u9500\u6bc1worker\n    for(i=0 ; i&lt;WORKERNUM ; ++i ) {\n        <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_worker_destroy<\/mark><\/strong>(&amp;worker&#91;i], &amp;pool) ;\n    }\n\n    \/\/ 8\u3001\u9500\u6bc1pool\n    <strong><mark style=\"background-color:#b1c5a4\" class=\"has-inline-color\">pthread_pool_destroy<\/mark><\/strong>(&amp;pool) ;\n\n    return 0 ;\n}\n\n<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);height:5px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n<\/div>\n\n\n\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/strong><\/p>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"876\" height=\"1003\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/1111-1.png\" alt=\"\" class=\"wp-image-691\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/1111-1.png 876w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/1111-1-262x300.png 262w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/06\/1111-1-768x879.png 768w\" sizes=\"auto, (max-width: 876px) 100vw, 876px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u4eab\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u5e93\uff0c\u70b9\u51fb\u4e0a\u8fb9\u7684\u6309\u94ae\u4e0b\u8f7d\u3002\u6e90\u4ee3\u7801\u5728 https:\/\/sourceforge.net\/projects\/pthread-pool\/ \uff0c\u7f16\u8bd1\u65f6\u62a5\u9519\uff0c\u6539\u4e86\u51e0\u884c\u4ee3\u7801\u3002<\/p>\n<p>\u7b2c\u4e00\u6b65\uff0c\u89e3\u538b: tar pzxvf pthread-pool.tar.gz\uff0c\u8fdb\u5165\u5de5\u4f5c\u76ee\u5f55: cd pthread-pool-0.0.2<\/p>\n<p>\u7b2c\u4e8c\u6b65\uff0c\u7f16\u8bd1\uff0c\u6267\u884c\u91cc\u8fb9\u7684\u811a\u672c\u5373\u53ef: bash autocompile.sh \u3002<\/p>\n<p>\u7b2c\u4e09\u6b65\uff0c\u5b89\u88c5\uff0c\u6267\u884c\u547d\u4ee4\uff1amake install\u3002\u52a8\u6001\u5e93libpthreadpool.so\u4f1a\u81ea\u52a8\u5b89\u88c5\u5230 \/usr\/local\/lib \u76ee\u5f55\uff0c\u5934\u6587\u4ef6\u5219\u5728 \/usr\/local\/include \u76ee\u5f55\u3002\u7f16\u8bd1\u7684\u65f6\u5019\u53ef\u4ee5\u6307\u5b9a \u5934\u6587\u4ef6\u548cso\u5e93\u7684\u5730\u5740\uff1b\u8fd0\u884c\u7684\u65f6\u5019\u5982\u679c\u627e\u4e0d\u5230 so \u5e93\uff0c\u53ef\u4ee5\u5728 \/etc\/ld.so.conf.d\/ \u76ee\u5f55\u4e0b\u968f\u4fbf\u52a0\u4e00\u4e2a xxx.conf\u7684\u6587\u4ef6\uff0c\u91cc\u8fb9\u5185\u5bb9\u662f \/usr\/local\/lib\/ \uff0c\u7136\u540e\u6267\u884c ldconfig\uff0c\u91cd\u65b0\u52a0\u8f7d\u52a8\u6001\u5e93\u67e5\u627e\u8def\u5f84\u3002<\/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":[254,253,261,178,257,255,256,262],"class_list":["post-672","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-pool","tag-pthread","tag-261","tag-178","tag-257","tag-255","tag-256","tag-262"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/672","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=672"}],"version-history":[{"count":12,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":901,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/672\/revisions\/901"}],"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=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}