{"id":302,"date":"2025-09-07T14:41:28","date_gmt":"2025-09-07T06:41:28","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=302"},"modified":"2025-11-24T16:37:24","modified_gmt":"2025-11-24T08:37:24","slug":"%e7%ba%bf%e7%a8%8b%e4%ba%92%e6%96%a5%e4%b8%8e%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=302","title":{"rendered":"\u7ebf\u7a0b\u4e92\u65a5\u4e0e\u540c\u6b65"},"content":{"rendered":"\n<p id=\"uacafd12f\">\u4f17\u6240\u5468\u77e5\uff0c\u7ebf\u7a0b\u662f\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u7684\uff0c\u7ebf\u7a0b\u4f1a\u5171\u4eab\u5927\u90e8\u5206\u8d44\u6e90\uff0c\u6240\u4ee5\u4f1a\u9020\u6210\u6570\u636e\u4e0d\u4e00\u81f4\u95ee\u9898\uff0c\u8fd9\u4e2a\u591a\u7ebf\u7a0b\u6267\u884c\u529b\u6d41\u88ab\u4fdd\u62a4\u7684\u5171\u4eab\u8d44\u6e90\u7684\u4ee3\u7801\uff0c\u5c31\u53eb\u4f5c\u4e34\u754c\u8d44\u6e90\uff0c\u800c\u6bcf\u4e2a\u7ebf\u7a0b\u5185\u90e8\uff0c\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u7684\u4ee3\u7801\uff0c\u5c31\u53eb\u505a\u4e34\u754c\u533a<\/p>\n\n\n\n<p id=\"u6b1dd993\">\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<pre id=\"lRNV9\" class=\"wp-block-code\"><code>#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n#include <pthread.h>\nint ticket = 100;\nvoid *route(void *arg)\n{\n\tchar *id = (char*)arg;\n\twhile ( 1 ) {\n\t\tif ( ticket > 0 ) {\n\t\t\tusleep(1000);\n\t\t\tprintf(\"%s sells ticket:%d\\n\", id, ticket);\n\t\t\tticket--;\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n}\nint main( void )\n{\n\tpthread_t t1, t2, t3, t4;\n\tpthread_create(&t1, NULL, route, (void*)\"thread 1\");\n\tpthread_create(&t2, NULL, route, (void*)\"thread 2\");\n\tpthread_create(&t3, NULL, route, (void*)\"thread 3\");\n\tpthread_create(&t4, NULL, route, (void*)\"thread 4\");\n\tpthread_join(t1, NULL);\n\tpthread_join(t2, NULL);\n\tpthread_join(t3, NULL);\n\tpthread_join(t4, NULL);\n} \n\u4e00\u6b21\u6267\u2f8f\u7ed3\u679c\uff1a\nthread 4 sells ticket:100\n\t...\n\tthread 4 sells ticket:1\n\tthread 2 sells ticket:0\n\tthread 1 sells ticket:-1\n\tthread 3 sells ticket:-2<\/code><\/pre>\n\n\n\n<p id=\"u51d906cf\">\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\u7968\u5df2\u7ecf\u53d8\u6210\u8d1f\u6570\u4e86<br>\u6240\u4ee5\u4e92\u65a5\u662f\u4efb\u4f55\u65f6\u523b\uff0c\u4e92\u65a5\u4fdd\u8bc1\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u6267\u884c\u6d41\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\uff0c\u901a\u5e38\u5bf9\u4e34\u754c\u8d44\u6e90\u8d77\u4fdd\u62a4\u4f5c\u7528<br>\u6240\u4ee5\u6211\u4eec\u8981\u5982\u4f55\u53bb\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5462\uff1f\u6211\u4eec\u5c31\u5f15\u5165\u9501\u7684\u6982\u5ff5<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"GLHj9\">\u9501<\/h1>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.nlark.com\/yuque\/0\/2025\/png\/50466557\/1757150834669-2827ccb0-498f-4744-9ef6-889a4372a184.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<p id=\"uc0f9735f\">\u9501\u4e5f\u662fpthread\u5e93\u63d0\u4f9b<\/p>\n\n\n\n<p id=\"u4c2e1cf9\"><strong>\u5b9a\u4e49\u9501<\/strong><\/p>\n\n\n\n<p id=\"u831526a2\">1.\u9759\u6001\u5206\u914d:<br>pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER<br>2\u52a8\u6001\u5206\u914d\uff1a<\/p>\n\n\n\n<p id=\"u6e635c13\">int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);<\/p>\n\n\n\n<p id=\"ued4c4efe\">\u53c2\u6570\uff1a<br>mutex\uff1a\u8981\u521d\u59cb\u5316\u7684\u4e92\u65a5\u91cf<\/p>\n\n\n\n<p id=\"u8d0a1d6b\">attr\uff1aNULL<\/p>\n\n\n\n<p id=\"u1bf60945\">\u6ce8\u610f\uff1a\u52a8\u6001\u5206\u914d\u9700\u8981\u53bb\u91ca\u653e\u9501\uff1aint pthread_mutex_destroy(pthread_mutex_t *mutex)\uff1b<br><strong>\u4f7f\u7528\u9501<\/strong><br>\u52a0\u9501\uff1a\u7ade\u4e89\u7533\u8bf7\u9501\uff0c\u591a\u7ebf\u7a0b\u9501\u88ab\u672c\u6765\u5c31\u662f\u4e34\u754c\u8d44\u6e90\uff0c\u6240\u4ee5\u7533\u8bf7\u9501\u7684\u8fc7\u7a0b\u5fc5\u987b\u662f\u539f\u5b50\u7684<\/p>\n\n\n\n<p id=\"u7a60fe23\">\u6210\u529f\uff1a\u7ee7\u7eed\u5411\u540e\u8fd0\u884c\uff0c\u8bbf\u95ee\u4e34\u754c\u533a\u4ee3\u7801\uff0c\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90<\/p>\n\n\n\n<p id=\"ub5b3dc8c\">\u5931\u8d25\uff1a\u963b\u585e\u6302\u8d77\u7533\u8bf7\u6267\u884c\u6d41<\/p>\n\n\n\n<p id=\"u70d6424e\">\u52a0\u9501\u4e0e\u89e3\u9501\uff1a<\/p>\n\n\n\n<p id=\"uee04a5c1\">int pthread_mutex_lock(pthread_mutex_t *mutex);<\/p>\n\n\n\n<p id=\"uacafd12f\">int pthread_mutex_unlock(pthread_mutex_t *mutex);<\/p>\n\n\n\n<p id=\"u3ec28f30\">\u8fd4\u56de\u503c:\u6210\u529f\u8fd4\u56de0,\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u53f7<br>int pthread_mutex_trylock(pthread_mutex_t *mutex);\u7533\u8bf7\u975e\u963b\u585e\u7248\u672c<\/p>\n\n\n\n<p id=\"u609db9c2\"><strong>\u9501\u63d0\u4f9b\u7684\u672c\u8d28<\/strong>\uff1a\u6267\u884c\u4e34\u754c\u533a\u4ee3\u7801\u7531\u5e76\u884c\u8f6c\u5316\u4e3a\u4e32\u884c<\/p>\n\n\n\n<p id=\"u17513788\"><strong>\u9501\u7684\u539f\u7406\uff1a<\/strong><\/p>\n\n\n\n<p id=\"u9c1a330d\">\u786c\u4ef6\u5b9e\u73b0\uff1a\u5173\u95ed\u65f6\u949f\u4e2d\u65ad<\/p>\n\n\n\n<p id=\"u2b677cfb\">\u8f6f\u4ef6\u5b9e\u73b0\uff1a\u4e3a\u4e86\u5b9e\u73b0\u4e92\u65a5\u9501\u64cd\u4f5c\uff0c\u5927\u591a\u6570\u4f53\u7cfb\u7ed3\u6784\u90fd\u662f\u4f9b\u4e86swap\u6216exchange\u6307\u4ee4\uff0c<\/p>\n\n\n\n<p id=\"uf67715cb\">\u8be5\u6307\u4ee4\u7684\u4f5c\u7528\u662f\u628a\u5bc4\u5b58\u5668\u548c\u5185\u5b58\u5355\u5143\u7684\u6570\u636e\u76f8\u4ea4\u6362<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.nlark.com\/yuque\/0\/2025\/png\/50466557\/1757161677745-eb3ee14a-b656-439e-b288-48033f99ff01.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<p id=\"uae05267e\">\u6211\u4eec\u7528swap,exchange\u5c06\u5185\u5b58\u4e2d\u7684\u53d8\u91cf\uff0c\u4ea4\u6362\u5230cpu\u7684\u5bc4\u5b58\u5668\u4e2d\uff1a<\/p>\n\n\n\n<p id=\"uf2b9d828\">\u672c\u8d28\u662f\u68c0\u6d4bal\u91cc\u9762\u7684\u5185\u5bb9\u770b\u662f\u5426>0,\u4e0d\u6ee1\u8db3\u6761\u4ef6\u5c31\u6302\u8d77\uff0c\u7136\u540e\u8ba9\u6ee1\u8db3\u6761\u4ef6\u7684\u53bb\u5207\u6362\u56de\u6765<\/p>\n\n\n\n<p id=\"u5d0d166a\">\u9762\u5411\u5bf9\u8c61\u53bb\u5c01\u88c5\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre id=\"TInx2\" class=\"wp-block-code\"><code>#pragma once\n#include<iostream>\n#include<pthread.h>\nnamespace MutexMudole{\n    class Mutex{\n        private:\n        pthread_mutex_t mutex;\n        public:\n        Mutex(){\n            pthread_mutex_init(&mutex,NULL);\n        }\n        ~Mutex(){\n            pthread_mutex_destroy(&mutex);\n        }\n        void Lock(){\n            pthread_mutex_lock(&mutex);\n        }\n        void UnLock(){\n            pthread_mutex_unlock(&mutex);\n        }\n    };\n}<\/code><\/pre>\n\n\n\n<p id=\"ua052c755\">\u6b64\u65f6\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e86<\/p>\n\n\n\n<p id=\"u9e05e3ff\">\u4f46\u662f\u6b64\u65f6\u6211\u4eec\u80fd\u4e0d\u80fd\u8ba9\u4ed6\u81ea\u5df1\u91ca\u653e\u5462\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u53bb\u91ca\u653e\u9501\uff0c\u6b64\u65f6\u6211\u4eec\u5c31\u53ea\u9700\u8981\u518d\u52a0\u4e00\u4e2a\u7c7b\u5c31\u53ef\u4ee5\u5b8c\u6210\u4e0b\u9762\u9700\u6c42<\/p>\n\n\n\n<pre id=\"aoAOY\" class=\"wp-block-code\"><code>class LockGuard{\n        public:\n        LockGuard(Mutex& mutex):mutex_(mutex){\n            mutex_.Lock();\n        }\n        ~LockGuard(){\n            mutex_.UnLock();\n        }\n        private:\n        Mutex& mutex_;\n    };<\/code><\/pre>\n\n\n\n<p id=\"u8196596d\">\u6240\u4ee5\u539f\u6765\u7684\u4ee3\u7801\u5c31\u53d8\u6210\u4e86\u8fd9\u6837\uff08\u4e0b\u9762\uff09,\u5c31\u53ef\u4ee5\u5b9e\u73b0\u52a0\u9501\u64cd\u4f5c<\/p>\n\n\n\n<pre id=\"Hekyt\" class=\"wp-block-code\"><code>#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n#include<iostream>\n#include<string>\n#include <pthread.h>\n#include\"Mutex.hpp\"\nusing namespace MutexMudole;\n\/\/pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;\nint ticket = 100;\nclass ThreadData{\n    public:\n    std::string name;\n    Mutex *lockp;\n    ThreadData(const std::string& name,Mutex &lockp):name(name),lockp(&lockp) {}\n};\nvoid *route(void *arg)\n{\n\tThreadData *td=(ThreadData*)arg;\n\twhile ( 1 ) {\n\t\tLockGuard guard(*td->lock);\/\/\u52a0\u9501\u5b8c\u6210\u3002RAII\u98ce\u683c\u7684\u5b9e\u73b0\u4e92\u65a5\u9501\n\t\tif ( ticket > 0 ) {\n\t\t\tusleep(1000);\n\t\t\tprintf(\"%s sells ticket:%d\\n\", td->name.c_str(), ticket);\n\t\t\tticket--;\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n}\nint main( void )\n{\n\tMutex lock;\n\tpthread_t t1, t2, t3, t4;\n    ThreadData *td1=new ThreadData(\"thread 1\",lock);\n    ThreadData *td2=new ThreadData(\"thread 2\",lock);\n    ThreadData *td3=new ThreadData(\"thread 3\",lock);\n    ThreadData *td4=new ThreadData(\"thread 4\",lock);\n\tpthread_create(&t1, NULL, route, (void*)td1);\n\tpthread_create(&t2, NULL, route, (void*)td2);\n\tpthread_create(&t3, NULL, route, (void*)td3);\n\tpthread_create(&t4, NULL, route, (void*)td4);\n\tpthread_join(t1, NULL);\n\tpthread_join(t2, NULL);\n\tpthread_join(t3, NULL);\n\tpthread_join(t4, NULL);\n    return 0;\n} \n<\/code><\/pre>\n\n\n\n<p id=\"u94be2d55\">LockGuard guard(*td->lock),\u6b64\u65f6\u5c31\u53ea\u9700\u8981\u53bb\u4f7f\u7528\u8fd9\u4e2a\u4e34\u65f6\u53d8\u91cf\u5c31\u53ef\u4ee5\u5b8c\u6210\u52a0\u9501\u64cd\u4f5c<\/p>\n\n\n\n<p id=\"ue57f9374\">\u4e0a\u9762\u4e5f\u5c31\u662fc++\u91cc\u9762lock_guard\u548cmutex\u7684\u5e95\u5c42\u539f\u7406<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"iG8Qe\">\u4e8c.\u7ebf\u7a0b\u540c\u6b65<\/h1>\n\n\n\n<p id=\"u47d0bb6f\"><strong>\u540c\u6b65\uff1a<\/strong><\/p>\n\n\n\n<p id=\"u6eb46d84\">\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u9ad8\u9891\u7684\u7533\u8bf7\u9501\uff0c\u4f1a\u9020\u6210\u7ebf\u7a0b\u9965\u997f\u95ee\u9898,\u6240\u4ee5\u8ba9\u7ebf\u7a0b\u5b89\u5168\u7684\u524d\u63d0\u4e0b\u8ba9\u6240\u6709\u6267\u884c\u6d41\uff0c\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\uff0c\u6309\u7167\u4e00\u5b9a\u7684\u987a\u5e8f\u8fdb\u884c\u8bbf\u95ee\u8d44\u6e90\u5c31\u53eb\u4f5c\uff1a<strong>\u540c\u6b65<\/strong><\/p>\n\n\n\n<p id=\"u240b54ed\"><strong>\u6761\u4ef6\u53d8\u91cf\uff1a<\/strong><\/p>\n\n\n\n<p id=\"ue5004e85\">\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u4e92\u65a5\u5730\u8bbf\u95ee\u5176\u4e2a\u53d8\u91cf\u65f6\uff0c\u5b83\u53ef\u80fd\u53d1\u73b0\u5728\u5176\u5b83\u7ebf\u7a0b\u6539\u53d8\u72b6\u6001\u4e4b\u524d\uff0c\u5b83\u4ec0\u4e48\u4e5f\u505a\u4e0d\u4e86\u3002<\/p>\n\n\n\n<p id=\"u2a038b99\">\u4f8b\u5982\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u961f\u5217\u65f6\uff0c\u53d1\u73b0\u961f\u5217\u4e3a\u7a7a\uff0c\u5b83\u53ea\u80fd\u7b49\u5f85\uff0c\u53ea\u5230\u5176\u5b83\u7ebf\u7a0b\u5c06\u4e00\u4e2a\u8282\u70b9\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u3002\u8fd9\u79cd\u60c5\u51b5\u5c31<\/p>\n\n\n\n<p id=\"ufca544d3\">\u9700\u8981\u7528\u5230\u6761\u4ef6\u53d8\u91cf\u3002<\/p>\n\n\n\n<p id=\"u36a9bed0\">\u6761\u4ef6\u53d8\u91cf\u63a5\u53e3\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.nlark.com\/yuque\/0\/2025\/png\/50466557\/1757211011981-b9e21c15-7793-485e-ad6f-0583aff6b84f.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<p id=\"u9957752b\">\u4ed6\u4e0e\u9501\u6709\u4e9b\u76f8\u4f3c\uff0c\u4f46\u6761\u4ef6\u53d8\u91cf\u662f\u7528\u6765\u4fdd\u8bc1\u7ebf\u7a0b\u540c\u6b65\u7684\uff0c\u9501\u662f\u4fdd\u8bc1\u7ebf\u7a0b\u4e92\u65a5\u7684<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.nlark.com\/yuque\/0\/2025\/png\/50466557\/1757211387147-07f97fb5-0a60-479d-b9a6-ebbe2df69a69.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<p id=\"u8528bb57\">\u7b2c\u4e00\u4e2a\u662f\u5524\u9192\u6307\u5b9a\u53d8\u91cf\u4e2d\u7684\u6240\u6709\u7ebf\u7a0b\uff0c\u7b2c\u4e8c\u4e2a\u63a5\u53e3\u5219\u662f\u5524\u9192\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0b<\/p>\n\n\n\n<p id=\"u9d960bce\">\u4e0b\u9762\u662f\u4e00\u4e2a\u5c0fdemo:<\/p>\n\n\n\n<pre id=\"VFk5B\" class=\"wp-block-code\"><code>#include<iostream>\n#include<pthread.h>\n#include<vector>\n#include<string.h>\n#include<unistd.h>\npthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;\npthread_cond_t cond=PTHREAD_COND_INITIALIZER;\n#define NUM 5\nint cnt=1000;\nvoid* thread_func(void*arg){\n    std::string name=(char*)arg;\n    while(true){\n        pthread_mutex_lock(&lock);\n        \/\/\u76f4\u63a5\u8ba9\u7ebf\u7a0b\u8fdb\u884c\u7b49\u5f85\n        pthread_cond_wait(&cond,&lock);\n        \/\/\u7ebf\u7a0b\u88ab\u5524\u9192\u540e\uff0c\u8fdb\u884c\u64cd\u4f5c\n        std::cout<<\" wake up\"<<std::endl;\n        std::cout<<name<<\" cnt \"<<cnt<<std::endl;\n        cnt--;\n        pthread_mutex_unlock(&#038;lock);\n    }\n}\nint main(){\n    std::vector<pthread_t> threads;\n    for(int i=0;i<5;i++){\n        pthread_t tid;\n        std::string name=\"thread\"+std::to_string(i);\n       int ret=pthread_create(&#038;tid,NULL,thread_func,(void*)name.c_str());\n       if(ret!=0){\n           continue;\n       }\n       threads.push_back(tid);\n       sleep(1);\n    }\n    while(true){\n        \/\/pthread_cond_signal(&#038;cond);\n        pthread_cond_broadcast(&#038;cond);\n        sleep(1);\n    }\n    for(auto&#038;tid:threads){\n        int m=pthread_join(tid,NULL);\n    }\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"FCqJF\">\u4e09.\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\uff1a<\/h1>\n\n\n\n<p id=\"uf9ee837b\"><strong>\u672c\u8d28<\/strong>\uff1a\u591a\u7ebf\u7a0b\u901a\u4fe1<\/p>\n\n\n\n<p id=\"u448fd323\"><strong>\u7406\u89e3<\/strong>\uff1a<\/p>\n\n\n\n<p id=\"ueb2108a9\">\u751f\u4ea7\u8005\u548c\u751f\u4ea7\u8005\uff1a\u7ade\u4e89\u5173\u7cfb\uff0c\u4e92\u65a5\u5173\u7cfb<\/p>\n\n\n\n<p id=\"ud7e48c0e\">\u6d88\u8d39\u8005\u548c\u6d88\u8d39\u8005: \u4e92\u65a5\u5173\u7cfb<\/p>\n\n\n\n<p id=\"u9855e34b\">\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\uff1a\u4e92\u65a5\u548c\u540c\u6b65<\/p>\n\n\n\n<p id=\"u71773b21\">2\u4e2d\u89d2\u8272\uff1a\u751f\u4ea7\u8005\u89d2\u8272\u548c\u6d88\u8d39\u8005\u89d2\u8272\uff08\u7ebf\u7a0b\u627f\u62c5\uff09<\/p>\n\n\n\n<p id=\"u5e4852f3\">1\u4e2a\u4ea4\u6613\u573a\u6240\uff1a\u4ee5\u7279\u5b9a\u7ed3\u6784\u6784\u6210\u7684\u4e00\u76f4\"\u5185\u5b58\"\u7a7a\u95f4<\/p>\n\n\n\n<p id=\"u2e7d42cf\"><strong>\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b\u7684\u597d\u5904<\/strong><\/p>\n\n\n\n<p id=\"u09c29137\">1.\u751f\u4ea7\u8fc7\u7a0b\u548c\u6d88\u8d39\u8fc7\u7a0b\u89e3\u8026<\/p>\n\n\n\n<p id=\"u5b985c96\">2.\u652f\u6301\u5fd9\u95f2\u4e0d\u5747<\/p>\n\n\n\n<p id=\"uf7d70f56\">3.\u63d0\u9ad8\u6548\u7387<\/p>\n\n\n\n<p id=\"u0b055ed1\">\u4f8b\u5b50\uff1a\u7ba1\u9053\u95f4\u901a\u4fe1\uff0c\u4e00\u4e2a\u5199\u4e00\u4e2a\u8bfb<\/p>\n\n\n\n<p id=\"ueca9d39e\">\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<p id=\"ucd9023ea\"><strong>\u57fa\u4e8eblockqueue\u7684\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u6a21\u578b<\/strong><\/p>\n\n\n\n<p id=\"u626db165\">\u963b\u585e\u961f\u5217\u56fe\uff1a<\/p>\n\n\n\n<p id=\"ufc3eb096\">\u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\u963b\u84b8\u961f\u5217\uff08blockingqueue)\u662f\u4e00\u79cd\u5e38\u7528\u4e8e\u5b9e\u73b0\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u6a21\u578b\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n\n\n\n<p id=\"ufd3c264c\">\u5176\u4e0e\u666e\u901a\u7684\u961f\u5217\u533a\u522b\u5728\u4e8e\uff0c\u5f53\u961f\u5217\u4e3a\u7a7a\u65f6\uff0c\u4ece\u961f\u5217\u83b7\u53d6\u5143\u7d22\u7684\u64cd\u4f5c\u5c06\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u961f\u5217\u4e2d\u88ab\u653e<\/p>\n\n\n\n<p id=\"u413a1838\">\u5165\u4e86\u5143\u7d20\uff1b\u5f53\u961f\u5217\u6ee1\u65f6\uff0c\u5f80\u961f\u5217\u91cc\u5b58\u653e\u5143\u7d20\u7684\u64cd\u4f5c\u4e5f\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u6709\u5143\u7d20\u88ab\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\uff08\u4e0a<\/p>\n\n\n\n<p id=\"u25d840a7\">\u7684\u64cd\u4f5c\u90fd\u662f\u57fa\u4e8e\u4e0d\u540c\u7684\u7ebf\u7a0b\u6765\u8bf4\u7684\uff0c\u7ebf\u7a0b\u5728\u5bf9\u963b\u585e\u961f\u5217\u8fdb\u7a0b\u64cd\u4f5c\u65f6\u4f1a\u88ab\u963b\u585e\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.nlark.com\/yuque\/0\/2025\/png\/50466557\/1757217502472-9a701be3-28d8-4be0-b8b4-eb027e0bcda9.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<p id=\"u63651a9f\">\u963b\u585e\u961f\u5217\u662f\u4e00\u4e2a\u5bb9\u91cf\u5177\u6709\u4e0a\u9650\u7684\u961f\u5217\u3002\u4e0d\u6ee1\u8db3\u8bfb\u5199\u6761\u4ef6\u65f6\uff0c\u4f1a\u88ab\u963b\u585e<\/p>\n\n\n\n<pre id=\"p2rvd\" class=\"wp-block-code\"><code> #pragma once\n#include<iostream>\n#include<unistd.h>\n#include<pthread.h>\n#include<queue>\nconst int default_capacity=5;\ntemplate<typename T>\nclass BlockQueue{\npublic:\n     BlockQueue(int cap=default_capacity):_cap(cap),csleep_num(0),psleep_num(0){\n        pthread_mutex_init(&_mutex,NULL);\n        pthread_cond_init(&_full_cond,NULL);\n        pthread_cond_init(&_empty_cond,NULL);\n     }\n     void Equeue(const T&val){\n        \/\/\u751f\u4ea7\u8005\u8c03\u7528\n        pthread_mutex_lock(&_mutex);\n        while(IsFull()){\n            \/\/\u963b\u585e\u7b49\u5f85\n            \/\/\u5f53\u88ab\u5524\u9192\u65f6\u5fc5\u987b\u8fd8\u8981\u7ade\u4e89_mutex\u8fd9\u628a\u9501\u624d\u80fd\u5411\u4e0b\u8fd0\u884c\n            psleep_num++;\n            \/\/\u4f2a\u5524\u9192\uff1a\u6761\u4ef6\u4e0d\u6ee1\u8db3\u4e5f\u88ab\u5524\u9192\u4e86\n            pthread_cond_wait(&_full_cond,&_mutex);\n            psleep_num--;\n        }\n        \/\/\u4f7f\u7528while\u518d\u68c0\u67e5\u4e00\u6b21100%\u786e\u5b9a\u4e0d\u662f\u4f2a\u5524\u9192\n            \/\/\u5165\u961f\n            q.push(val);\n            \/\/\u901a\u77e5\u6d88\u8d39\u8005\n            if(csleep_num>0){\n                pthread_cond_signal(&_empty_cond);\n            }\n            pthread_mutex_unlock(&_mutex);\n     }\n     T Pop(){\n        \/\/\u6d88\u8d39\u8005\u8c03\u7528\n        pthread_mutex_lock(&_mutex);\n        while(IsEmpty()){\n            \/\/\u963b\u585e\u7b49\u5f85\n            csleep_num++;\n            pthread_cond_wait(&_empty_cond,&_mutex);\n            csleep_num--;\n        }\n            \/\/\u51fa\u961f\n            T val=q.front();\n            q.pop();\n            \/\/\u901a\u77e5\u751f\u4ea7\u8005\n            if(psleep_num>0){\n                pthread_cond_signal(&_full_cond);\n            }\n            pthread_mutex_unlock(&_mutex);\n            return val;\n     }\n     ~BlockQueue(){\n        pthread_mutex_destroy(&_mutex);\n        pthread_cond_destroy(&_full_cond);\n        pthread_cond_destroy(&_empty_cond);\n     }\nprivate:\n     bool IsFull(){\n        return q.size()==_cap;\n     }\n     bool IsEmpty(){\n        return q.empty();\n     }\n    std::queue<T> q;\n    int _cap;\/\/\u5bb9\u91cf\u5927\u5c0f\n    pthread_mutex_t _mutex;\n    pthread_cond_t _full_cond;\/\/\u751f\u4ea7\u8005\u5524\u9192\u6761\u4ef6\u53d8\u91cf\n    pthread_cond_t _empty_cond;\/\/\u6d88\u8d39\u8005\u5524\u9192\u6761\u4ef6\u53d8\u91cf\n    int csleep_num;\/\/\u6d88\u8d39\u8005\u7684\u4e2a\u6570\n    int psleep_num;\/\/\u751f\u4ea7\u8005\u7684\u4e2a\u6570\n};<\/code><\/pre>\n\n\n\n<p id=\"ud25a59e9\">\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u63d0\u9ad8\u751f\u4ea7\u6548\u7387\u4f53\u73b0\u5728 \u4e8e\u672a\u6765\u83b7\u53d6\u4efb\u52a1\u548c\u5904\u7406\u5177\u4f53\u4efb\u52a1\uff0c\u662f\u5e76\u53d1<\/p>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5\uff0c\u7ebf\u7a0b\u662f\u5171\u4eab\u5730\u5740\u7a7a\u95f4\u7684\uff0c\u7ebf\u7a0b\u4f1a\u5171\u4eab\u5927\u90e8\u5206\u8d44\u6e90\uff0c\u6240\u4ee5\u4f1a\u9020\u6210\u6570\u636e\u4e0d\u4e00\u81f4\u95ee\u9898\uff0c\u8fd9\u4e2a\u591a\u7ebf\u7a0b\u6267\u884c\u529b\u6d41\u88ab\u4fdd\u62a4\u7684\u5171\u4eab&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4,5],"tags":[],"class_list":["post-302","post","type-post","status-publish","format-standard","hentry","category-c","category-linux"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=302"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/302\/revisions"}],"predecessor-version":[{"id":842,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/302\/revisions\/842"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}