{"id":1273,"date":"2024-09-19T00:30:08","date_gmt":"2024-09-18T16:30:08","guid":{"rendered":"https:\/\/www.madbull.site\/?p=1273"},"modified":"2024-10-10T14:48:57","modified_gmt":"2024-10-10T06:48:57","slug":"%e5%be%aa%e7%8e%af%e9%93%be%e8%a1%a8-libhl%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=1273","title":{"rendered":"\u5faa\u73af\u94fe\u8868&#8212;libhl\u5e93\u7684\u4f7f\u7528\uff08\u4e00\uff09"},"content":{"rendered":"\n<p>\u672c\u6b21\u793a\u4f8b\u4e3a<strong>\u65e0\u9501\u7ebf\u7a0b\u5b89\u5168\u5faa\u73af\u94fe\u8868<\/strong>\u7684\u4f7f\u7528\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>libhl\u5f00\u6e90\u5e93\u7f16\u8bd1\u5b89\u88c5\u8fc7\u7a0b\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1a<a href=\"https:\/\/www.madbull.site\/?p=897\">https:\/\/www.madbull.site\/?p=897<\/a> C\u8bed\u8a00\u5b9e\u73b0\u57fa\u7840\u6570\u636e\u7ed3\u6784\u5e93<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\u4ee3\u7801<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u672c\u6b21\u793a\u4f8b\u8bbe\u8ba1\u4e86\u4e00\u4e2a\u62a2\u5360\u5f0f\u4efb\u52a1\u5904\u7406\u7684\u529f\u80fd\u3002\u51711\u4e2a\u4e3b\u7ebf\u7a0b\u548c5\u4e2a\u4efb\u52a1\u5904\u7406\u7ebf\u7a0b\uff0c\u4e3b\u7ebf\u7a0b\u5411\u961f\u5217 task_q \u4e2d\u63a8\u9001 300 \u4e2a\u4efb\u52a1\uff0c5 \u4e2a\u4efb\u52a1\u5904\u7406\u7ebf\u7a0b\uff0c\u4ece task_q \u4e2d\u62a2\u5360\u5f0f\u7684\u83b7\u53d6\u4efb\u52a1\u5e76\u5904\u91cc\uff1b\u5e76\u4e14\u81ea\u5df1\u91ca\u653e\u5904\u7406\u5b8c\u7684\u4efb\u52a1\u7684\u5185\u5b58\u3002<\/p>\n\n\n\n<p>\u672c\u6b21\u6d4b\u8bd5\u8fd8\u6d89\u53ca <strong>\u961f\u5217\u6ee1\u4e86\u65b0\u6570\u636e\u5bf9\u65e7\u6570\u636e\u7684\u8986\u76d6\u5904\u7406<\/strong> \u548c <strong>\u961f\u5217\u9500\u6bc1\u65f6\u5185\u5b58\u91ca\u653e\u7684\u5904\u7406<\/strong>\u3002<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u521b\u5efa\u6587\u4ef6 trqueue.c \uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ trqueue.c\n\/\/ gcc -Wall trqueue.c -o trqueue -lhl\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n\n#include \"rqueue.h\"\n\n\n#define PTH_NUM 5\n\n\/\/ \u5047\u8bbe\u8fd9\u662f\u4e00\u4e2a\u4efb\u52a1\ntypedef struct task_t_ {\n    int num ;\n} task_t ;\n\n\/\/ \u58f0\u660e\u4efb\u52a1\u5904\u7406\u51fd\u6570\nvoid * task_deal(void * arg) ;\n\n\/\/ value\u6e05\u7406\u7684\u56de\u8c03\u51fd\u6570\uff0c\u6e05\u7406\u7533\u8bf7\u7684\u5185\u5b58\nvoid free_task_cb(void *v) ;\n\n\/\/ \u58f0\u660e\u4efb\u52a1\u5904\u7406\u94fe\u8868\nrqueue_t * task_q = NULL ;\n\nint main(int argc, char *argv&#91;]) {\n\n    pthread_t pids&#91;PTH_NUM] ;\n    int ids&#91;PTH_NUM] ;\n    int rc ;\n\n    \/\/ \u521b\u5efa\u94fe\u8868\u3002\u9009\u62e9\u8986\u76d6\u6a21\u5f0f\uff0c\u8d85\u8fc7\u94fe\u8868\u957f\u5ea6\uff0c\u5219\u628a\u65e7\u6570\u636e\u8986\u76d6\u6389\u3002\n    task_q = rqueue_create( 5, RQUEUE_MODE_OVERWRITE ) ;\n    if( task_q == NULL ) {\n        return -1 ;\n    }\n\n    \/\/ \u521b\u5efa\u94fe\u8868\u503c\u9500\u6bc1\u7684\u56de\u8c03\u51fd\u6570\uff0c\u6ea2\u51fa\u548c\u9500\u6bc1\u7684\u65f6\u5019\uff0c\u5bf9value\u503c\u7684\u5904\u7406\n    rqueue_set_free_value_callback(task_q, free_task_cb);\n\n    \/\/ \u521b\u5efa\u4efb\u52a1\u5904\u7406\u7ebf\u7a0b\n    for (int i=0; i &lt; PTH_NUM ; i++ ) {\n        ids&#91;i] = i ;\n        rc = pthread_create(&amp;pids&#91;i], NULL, task_deal, &amp;ids&#91;i]) ;\n        if (rc != 0 ) {\n            return -1 ;\n        }\n    }\n\n    task_t * tsk = NULL ;\n    int tid = 0 ;\n    while(tid &lt; 300) {\n        tsk = (task_t *)calloc(1, sizeof(task_t)) ;\n        tsk-&gt;num = tid++ ;\n        \/\/ \u5411\u94fe\u8868\u5199\u6570\u636e\n        rqueue_write(task_q, tsk) ;\n    }\n\n    for (int i=0; i &lt; PTH_NUM ; i++ ) {\n        rc = pthread_join(pids&#91;i], NULL) ;\n    }\n    \n    \/\/ \u9500\u6bc1\u94fe\u6761\n    rqueue_destroy(task_q);\n    return 0 ;\n}\n\n\n\/\/ \u94fe\u8868\u9500\u6bc1\u65f6\uff0c\u91ca\u653e value \u7a7a\u95f4\nvoid free_task_cb(void *v) {\n    if( v ) {\n        task_t * tsk = (task_t*) v ; \n        printf(\"free tsk &#91;%d]\\n\", tsk-&gt;num ) ;\n        free(v) ;\n    }\n}\n\n\/\/ \u4efb\u52a1\u5904\u7406\u51fd\u6570\nvoid * task_deal(void * arg) {\n    int id = *(int *) arg ;\n    task_t * tsk = NULL ;\n    int flag = 1 ;\n    while(flag) {\n        \/\/ \u5224\u65ad\u94fe\u8868\u662f\u5426\u4e3a\u7a7a\n        if( rqueue_isempty( task_q ) ) {\n            usleep(20) ;\n            continue ;\n        }\n\n        \/\/ \u83b7\u53d6\u4e00\u4e2a\u6570\u636e\n        tsk = rqueue_read(task_q) ;\n        if ( tsk == NULL ) {\n            usleep(20) ;\n            continue ;\n        }\n\n        if(tsk-&gt;num &gt;= 220 ) {\n            flag = 0 ;\n            \/\/ \u5927\u4e8e\u7b49\u4e8e220\u7684\u4efb\u52a1\u4e0d\u5904\u7406\uff0c\u6254\u56de\u4efb\u52a1\u961f\u5217\u4e2d\u3002\n            rqueue_write(task_q, tsk) ;\n            continue ;\n        }\n        \/\/ \u786e\u5b9e\u62ff\u5230\u4e86\u6570\u636e\n        printf(\"&#91;%d] thread get task &#91;%d]\\n\", id, tsk-&gt;num ) ;\n        free(tsk) ;\n    }\n    pthread_exit(NULL) ;\n    return NULL ;\n}<\/code><\/pre>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>\u6d4b\u8bd5\uff1a<\/strong><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u4e0b\u56fe\u4e2d\uff0c\u7ea2\u8272\u6846\u91cc\u7684\u6570\u636e\u662f\u56e0\u4e3a\u5faa\u73af\u94fe\u8868\u4e2d\u7684\u6570\u636e\u6301\u7eed\u589e\u52a0\uff0c\u6ca1\u6709\u53ca\u65f6 read \u51fa\u53bb\u5bfc\u81f4\u94fe\u8868\u6ee1\u4e86\uff0c\u6240\u4ee5<strong>\u6e05\u7406\u4e86\u65e7\u6570\u636e<\/strong>\u3002\u6b64\u6b21\u5728\u8c03\u7528 rqueue_create \u521b\u5efa\u94fe\u8868\u65f6\u65f6\u7ed9\u4e86\u53c2\u6570 5 \uff0c\u4e3a\u4e86\u6d4b\u8bd5\u5f02\u5e38\u5904\u7406\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-3-color\"><strong>\u6ce8\u610f\uff1a\u5728\u5b9e\u9645\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u63d0\u524d\u7533\u8bf7\u8db3\u591f\u7684\u7a7a\u95f4\u3002\u80af\u5b9a\u662f\u8fdc\u8fdc\u8d85\u8fc7 5 \u4e2a\u7f13\u5b58\u7684\u3002<\/strong><\/mark><\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" 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=\"515\" height=\"394\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/09\/Screenshot_2024-09-19_00-17-46.png\" alt=\"\" class=\"wp-image-1275\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/09\/Screenshot_2024-09-19_00-17-46.png 515w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/09\/Screenshot_2024-09-19_00-17-46-300x230.png 300w\" sizes=\"auto, (max-width: 515px) 100vw, 515px\" \/><\/figure>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>\u7a0b\u5e8f\u8fd0\u884c\u6700\u540e80\u884c\u662f\u5728\u9500\u6bc1 task_q \u65f6\uff0c\u81ea\u52a8\u8c03\u7528\u56de\u8c03\u51fd\u6570 free_task_cb \u91ca\u653e\u5185\u5b58\u3002\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<div style=\"margin-top:var(--wp--preset--spacing--10);margin-bottom:var(--wp--preset--spacing--10);height:0px\" 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=\"173\" height=\"430\" src=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/09\/\u56fe\u7247-29.png\" alt=\"\" class=\"wp-image-1311\" srcset=\"https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/09\/\u56fe\u7247-29.png 173w, https:\/\/www.madbull.site\/wp-content\/uploads\/2024\/09\/\u56fe\u7247-29-121x300.png 121w\" sizes=\"auto, (max-width: 173px) 100vw, 173px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6b21\u793a\u4f8b\u4e3a\u65e0\u9501\u7ebf\u7a0b\u5b89\u5168\u5faa\u73af\u94fe\u8868\u7684\u4f7f\u7528\u6d4b\u8bd5\u3002<br \/>\nlibhl\u5f00\u6e90\u5e93\u7f16\u8bd1\u5b89\u88c5\u8fc7\u7a0b\uff0c\u53c2\u770b\u5f80\u671f\u6587\u7ae0\uff1ahttps:\/\/www.madbull.site\/?p=897 C\u8bed\u8a00\u5b9e\u73b0\u57fa\u7840\u6570\u636e\u7ed3\u6784\u5e93<br \/>\n\u6d4b\u8bd5\u4ee3\u7801<br \/>\n\u521b\u5efa\u6587\u4ef6 trqueue.c \uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/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":[461,463,462,464,361],"class_list":["post-1273","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-c","category-154","tag-libhl","tag-463","tag-462","tag-464","tag-361"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1273","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=1273"}],"version-history":[{"count":11,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1273\/revisions"}],"predecessor-version":[{"id":1524,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/1273\/revisions\/1524"}],"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=1273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}