{"id":263350,"date":"2024-05-15T15:03:11","date_gmt":"2024-05-15T07:03:11","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/263350.html"},"modified":"2024-05-15T15:03:17","modified_gmt":"2024-05-15T07:03:17","slug":"c-%e8%af%ad%e8%a8%80%e4%bb%a3%e7%a0%81%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/263350.html","title":{"rendered":"C \u8bed\u8a00\u4ee3\u7801\u600e\u4e48\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27123630\/bb44a652-4108-41ef-b907-3112a18c9267.webp\" alt=\"C \u8bed\u8a00\u4ee3\u7801\u600e\u4e48\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\" \/><\/p>\n<p><p>\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u6d89\u53ca\u5230\u5bf9\u9759\u6001\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7ebf\u7a0b\u5b89\u5168\u6982\u5ff5\u7684\u5e94\u7528\u3002<strong>\u5355\u4f8b\u6a21\u5f0f\u4fdd\u8bc1\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u4e00\u4e2a\u7c7b\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u5168\u5c40\u8bbf\u95ee\u70b9<\/strong>\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u9759\u6001\u5168\u5c40\u53d8\u91cf\u3001\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7ed3\u5408\u51fd\u6570\u5b9e\u73b0\u3002\u7279\u522b\u5730\uff0c\u9700\u8981\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\uff0c\u9632\u6b62\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u521b\u5efa\u591a\u4e2a\u5b9e\u4f8b\u3002\u5176\u4e2d<strong>\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7ed3\u5408\u51fd\u6570\u5b9e\u73b0\u662f\u6700\u4e3a\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\uff0c\u56e0\u4e3a\u5b83\u65e2\u4fdd\u8bc1\u4e86\u61d2\u52a0\u8f7d\uff0c\u4e5f\u76f8\u5bf9\u5bb9\u6613\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e3a\u4ec0\u4e48\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5355\u4f8b\u6a21\u5f0f\u4f5c\u4e3a\u4e00\u79cd\u5e38\u7528\u7684\u8f6f\u4ef6\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e3b\u8981\u7528\u4e8e\u63a7\u5236\u8d44\u6e90\u8bbf\u95ee\uff0c\u786e\u4fdd\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\u3002\u5728C\u8bed\u8a00\u5f00\u53d1\u7684\u7cfb\u7edf\u6216\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u80fd\u5b58\u5728\u4e00\u4e9b\u8d44\u6e90\u6216\u914d\u7f6e\u9700\u8981\u5168\u5c40\u8bbf\u95ee\uff0c\u540c\u65f6\u53c8\u8981\u907f\u514d\u91cd\u590d\u521b\u5efa\u6d6a\u8d39\u8d44\u6e90\uff0c\u8fd9\u65f6\u5355\u4f8b\u6a21\u5f0f\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002\u6bd4\u5982\uff0c\u914d\u7f6e\u7ba1\u7406\u3001\u786c\u4ef6\u63a5\u53e3\u7ba1\u7406\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9759\u6001\u5168\u5c40\u53d8\u91cf\u5b9e\u73b0\u5355\u4f8b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u9759\u6001\u5168\u5c40\u53d8\u91cf\u5b9e\u73b0\u5355\u4f8b\u662f\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u5168\u5c40\u53d8\u91cf\u6765\u5b58\u50a8\u5b9e\u4f8b\uff0c\u7136\u540e\u901a\u8fc7\u4e00\u4e2a\u516c\u5f00\u7684\u63a5\u53e3\u51fd\u6570\u8fd4\u56de\u8fd9\u4e2a\u5b9e\u4f8b\u7684\u6307\u9488\u3002\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4e0d\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>#include&lt;stdio.h&gt;<\/p>\n<p>typedef struct {<\/p>\n<p>    \/\/ \u6210\u5458\u5b9a\u4e49...<\/p>\n<p>} Singleton;<\/p>\n<p>static Singleton* instance = NULL;<\/p>\n<p>Singleton* GetInstance() {<\/p>\n<p>    if (instance == NULL) {<\/p>\n<p>        instance = (Singleton*)malloc(sizeof(Singleton));<\/p>\n<p>        \/\/ \u521d\u59cb\u5316\u5b9e\u4f8b...<\/p>\n<p>    }<\/p>\n<p>    return instance;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u5b9e\u73b0\u5355\u4f8b<\/h3>\n<\/p>\n<p><p>\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684\u65b9\u6cd5\u901a\u8fc7\u51fd\u6570\u5185\u7684\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u6765\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\uff0c\u6b64\u65b9\u6cd5\u76f8\u6bd4\u9759\u6001\u5168\u5c40\u53d8\u91cf\u7684\u597d\u5904\u5728\u4e8e\u5b9e\u73b0\u4e86\u61d2\u52a0\u8f7d\uff0c\u5e76\u4e14\u5728C11\u4ee5\u540e\u7684\u5e76\u53d1\u73af\u5883\u901a\u8fc7\u7279\u5b9a\u7f16\u8bd1\u5668\u6307\u4ee4\u4e5f\u80fd\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>#include&lt;stdio.h&gt;<\/p>\n<p>typedef struct {<\/p>\n<p>    \/\/ \u6210\u5458\u5b9a\u4e49...<\/p>\n<p>} Singleton;<\/p>\n<p>Singleton* GetInstance() {<\/p>\n<p>    static Singleton instance;  \/\/ C11\u6807\u51c6\u786e\u4fdd\u4e86\u8fd9\u91cc\u7684\u7ebf\u7a0b\u5b89\u5168<\/p>\n<p>    return &amp;instance;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u7684\u5355\u4f8b\u6a21\u5f0f\u4e0d\u9700\u8981\u663e\u5f0f\u5730\u8c03\u7528\u91ca\u653e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u662f\u7531\u7f16\u8bd1\u5668\u81ea\u52a8\u7ba1\u7406\u7684\u9759\u6001\u6570\u636e\u3002\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c<strong>C11\u6807\u51c6<\/strong>\u4fdd\u8bc1\u4e86\u9759\u6001\u5c40\u90e8\u53d8\u91cf\u7684\u521d\u59cb\u5316\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\uff0c\u786e\u4fdd\u5355\u4f8b\u6a21\u5f0f\u7ebf\u7a0b\u5b89\u5168\u81f3\u5173\u91cd\u8981\u3002\u5c3d\u7ba1\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7684\u65b9\u6cd5\u5728C11\u6807\u51c6\u540e\u63d0\u4f9b\u4e86\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7684\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f46\u5728\u8001\u7248\u672c\u6807\u51c6\u6216\u8005\u5176\u4ed6\u7528\u4f8b\u4e0b\uff0c\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u7ebf\u7a0b\u5b89\u5168\u63aa\u65bd\uff0c\u5982\u4f7f\u7528\u4e92\u65a5\u9501\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-c\">\/\/ \u793a\u4f8b\u4ee3\u7801\uff0c\u5047\u8bbe\u73af\u5883\u975eC11\u6807\u51c6<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>#include &lt;pthread.h&gt;<\/p>\n<p>typedef struct {<\/p>\n<p>    \/\/ \u6210\u5458\u5b9a\u4e49...<\/p>\n<p>} Singleton;<\/p>\n<p>static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;<\/p>\n<p>Singleton* GetInstanceThread<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>() {<\/p>\n<p>    static Singleton* instance = NULL;<\/p>\n<p>    if (instance == NULL) {<\/p>\n<p>        pthread_mutex_lock(&amp;lock);<\/p>\n<p>        if (instance == NULL) {  \/\/ \u53cc\u91cd\u68c0\u67e5<\/p>\n<p>            instance = (Singleton*)malloc(sizeof(Singleton));<\/p>\n<p>            \/\/ \u521d\u59cb\u5316\u5b9e\u4f8b...<\/p>\n<p>        }<\/p>\n<p>        pthread_mutex_unlock(&amp;lock);<\/p>\n<p>    }<\/p>\n<p>    return instance;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3\u548c\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u7684\u76ee\u7684\u662f\u786e\u4fdd\u5168\u5c40\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u3002\u5728C\u8bed\u8a00\u4e2d\uff0c\u901a\u8fc7\u9759\u6001\u5168\u5c40\u53d8\u91cf\u6216\u5c40\u90e8\u9759\u6001\u53d8\u91cf\u7ed3\u5408\u51fd\u6570\u5b9e\u73b0\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u5e94\u7528\u573a\u666f\u8986\u76d6\u4e86\u914d\u7f6e\u7ba1\u7406\u3001\u786c\u4ef6\u63a5\u53e3\u7ba1\u7406\u7b49\u591a\u4e2a\u9886\u57df\u3002\u9009\u62e9\u4f55\u79cd\u5b9e\u73b0\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3001\u73af\u5883\uff08\u662f\u5426\u591a\u7ebf\u7a0b\uff09\u4ee5\u53ca\u6027\u80fd\u8003\u91cf\u3002<\/p>\n<\/p>\n<p><p><strong>\u5728\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u65f6\uff0c\u5e94\u8c28\u614e\u8003\u8651\u662f\u5426\u771f\u7684\u9700\u8981\u5168\u5c40\u552f\u4e00\u7684\u5b9e\u4f8b<\/strong>\u3002\u8fc7\u5ea6\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u80fd\u5bfc\u81f4\u7a0b\u5e8f\u7ed3\u6784\u590d\u6742\uff0c\u589e\u52a0\u8026\u5408\u5ea6\uff0c\u5f71\u54cd\u4ee3\u7801\u7684\u53ef\u6d4b\u8bd5\u6027\u548c\u7ef4\u62a4\u6027\u3002\u6b63\u786e\u7406\u89e3\u548c\u6070\u5f53\u5e94\u7528\u5355\u4f8b\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: C\u8bed\u8a00\u4e2d\u5982\u4f55\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\uff1f<\/strong><br \/>\nA: \u5355\u4f8b\u6a21\u5f0f\u5728C\u8bed\u8a00\u4e2d\u53ef\u4ee5\u901a\u8fc7\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u51fd\u6570\u5b9e\u73b0\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u9759\u6001\u53d8\u91cf\u6765\u4fdd\u5b58\u5b9e\u4f8b\u5316\u540e\u7684\u5bf9\u8c61\u3002\u7136\u540e\uff0c\u5728\u4e00\u4e2a\u9759\u6001\u51fd\u6570\u4e2d\u5224\u65ad\u8be5\u53d8\u91cf\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u5e76\u8d4b\u503c\u7ed9\u9759\u6001\u53d8\u91cf\uff0c\u5982\u679c\u4e0d\u4e3a\u7a7a\u5219\u76f4\u63a5\u8fd4\u56de\u8be5\u5b9e\u4f8b\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u5728\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u5b58\u5728\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u786e\u4fddC\u8bed\u8a00\u5355\u4f8b\u6a21\u5f0f\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\uff1f<\/strong><br \/>\nA: \u5728C\u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e92\u65a5\u9501\u6765\u786e\u4fdd\u5355\u4f8b\u6a21\u5f0f\u7684\u7ebf\u7a0b\u5b89\u5168\u6027\u3002\u5728\u9759\u6001\u51fd\u6570\u4e2d\uff0c\u5728\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u4ee3\u7801\u524d\u52a0\u4e0a\u4e92\u65a5\u9501\u7684\u52a0\u9501\u64cd\u4f5c\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u591f\u8fdb\u884c\u5bf9\u8c61\u7684\u5b9e\u4f8b\u5316\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5c06\u88ab\u963b\u585e\u3002\u5f53\u5b9e\u4f8b\u5316\u5b8c\u6bd5\u540e\uff0c\u91ca\u653e\u4e92\u65a5\u9501\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5373\u53ef\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<p><strong>Q: \u5355\u4f8b\u6a21\u5f0f\u5728C\u8bed\u8a00\u4e2d\u6709\u54ea\u4e9b\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff1f<\/strong><br \/>\nA: \u5355\u4f8b\u6a21\u5f0f\u5728C\u8bed\u8a00\u4e2d\u6709\u5f88\u591a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u3001\u65e5\u5fd7\u7cfb\u7edf\u3001\u914d\u7f6e\u7ba1\u7406\u5668\u7b49\u3002\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u5b9e\u4f8b\u5bf9\u8c61\u6765\u8fdb\u884c\u76f8\u5173\u64cd\u4f5c\uff0c\u901a\u8fc7\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u53ef\u4ee5\u4fdd\u8bc1\u5728\u6574\u4e2a\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\u5b58\u5728\uff0c\u907f\u514d\u4e86\u8d44\u6e90\u6d6a\u8d39\u548c\u7ebf\u7a0b\u5b89\u5168\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u5355\u4f8b\u6a21\u5f0f\u6d89\u53ca\u5230\u5bf9\u9759\u6001\u53d8\u91cf\u3001\u51fd\u6570\u548c\u7ebf\u7a0b\u5b89\u5168\u6982\u5ff5\u7684\u5e94\u7528\u3002\u5355\u4f8b\u6a21\u5f0f\u4fdd\u8bc1\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u4e00\u4e2a\u7c7b\u4ec5\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c [&hellip;]","protected":false},"author":3,"featured_media":263357,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/263350"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=263350"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/263350\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/263357"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=263350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=263350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=263350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}