{"id":463,"date":"2020-05-08T19:08:59","date_gmt":"2020-05-08T11:08:59","guid":{"rendered":"https:\/\/www.notown.club\/?p=463"},"modified":"2020-05-08T19:20:43","modified_gmt":"2020-05-08T11:20:43","slug":"c%e5%a4%9a%e7%ba%bf%e7%a8%8b%ef%bc%88%e7%ac%94%e8%ae%b0%e4%ba%8c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.notown.top\/?p=463","title":{"rendered":"C++\u591a\u7ebf\u7a0b\uff08\u7b14\u8bb0\u4e8c\uff09"},"content":{"rendered":"\n<p style=\"font-size:24px\"><strong>\u4e00\u3001\u5171\u4eab\u6570\u636e\u95ee\u9898<\/strong><\/p>\n\n\n\n<p>1\u3001\u591a\u4e2a\u7ebf\u7a0b<strong>\u53ea\u8bfb\uff0c\u662f\u5b89\u5168\u7a33\u5b9a<\/strong>\u7684<br>2\u3001\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5199\uff0c\u6216\u8005\u65e2\u6709\u8bfb\u7ebf\u7a0b\uff0c\u4e5f\u6709\u5199\u7ebf\u7a0b\uff0c\u82e5\u4e0d\u52a0\u5904\u7406\uff0c\u5c31\u4f1a\u51fa\u9519<\/p>\n\n\n\n<p>\u5904\u7406\u65b9\u6cd5\uff1a<br>\u8bfb\u7684\u65f6\u5019\u4e0d\u80fd\u5199\uff0c\u5199\u7684\u65f6\u5019\u4e0d\u80fd\u8bfb<br>\u6709\u4e24\u79cd\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5\uff1a<br>     \u2460\u5f15\u5165<strong>\u4e92\u65a5\u91cf<\/strong>\u7684\u6982\u5ff5\uff0c\u6bcf\u6b21\u8bfb\u5199\u6570\u636e\u524d\u90fd\u8fdb\u884c\u52a0\u9501\u4fdd\u62a4\uff0c\u5904\u7406\u5b8c\u6570\u636e\u540e\u89e3\u9501<br>     \u2461\u5f15\u5165<strong>\u539f\u5b50\u64cd\u4f5c<\/strong>\u7684\u6982\u5ff5\uff0c\u5b9a\u4e49\u67d0\u4e2a\u53d8\u91cf\u4e3a\u539f\u5b50\u7c7b\u578b\uff0c\u6bcf\u6b21\u8fdb\u884c\u4e00\u5143\u8fd0\u7b97\u7b26\u8fd0\u7b97\u65f6\uff0c\u90fd\u80fd\u786e\u4fdd\u5f53\u524d\u8fd0\u7b97\u4e0d\u88ab\u6253\u65ad\u3002<\/p>\n\n\n\n<p><strong>\u4e92\u65a5\u9501\u7684\u7279\u70b9\uff1a<\/strong><br>\u80fd\u5bf9<strong>\u4e00\u6bb5\u4ee3\u7801\u7247\u6bb5<\/strong>\u8fdb\u884c\u4fdd\u62a4\uff0c\u64cd\u4f5c<strong>\u7075\u6d3b<\/strong>\u53ef\u53d8\uff0c\u4f46\u52a0\u9501\u89e3\u9501\u6709\u4e00\u5b9a\u65f6\u95f4\u5f00\u9500\u3002<br><strong>\u539f\u5b50\u64cd\u4f5c\u7279\u70b9\uff1a<\/strong><br> \u4ec5\u9002\u7528\u4e8e\u4fdd\u62a4\u67d0\u4e2a\u53d8\u91cf\u7684<strong>\u4e00\u5143\u8fd0\u7b97\u7b26\u8fd0\u7b97<\/strong>\uff0c\u4f46\u76f8\u5bf9\u6765\u8bb2\u989d\u5916\u7684<strong>\u5f00\u9500\u5c0f<\/strong>\uff0c\u4e00\u822c\u53ea\u9002\u7528\u4e8e\u8ba1\u6570\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>A. \u4e92\u65a5\u9501\u7684\u7528\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<p><strong>1\u3001mutex\u7684\u6210\u5458\u51fd\u6570lock()\uff0cunlock()<\/strong><br>\u58f0\u660e\u4e86\u4e00\u4e2amutex\u53d8\u91cfa\u540e\uff0c\u53ef\u4ee5\u7528a.lock(),a.unlock()\u8fdb\u884c\u52a0\u9501\u89e3\u9501\uff0c<strong>\u52a0\u9501\u548c\u89e3\u9501\u7684\u6b21\u6570\u5fc5\u987b\u76f8\u7b49<\/strong>\u3002\u52a0\u9501\u671f\u95f4\u80fd\u4fdd\u8bc1\u5f53\u524d\u7ebf\u7a0b\u7684\u64cd\u4f5c\u4e0d\u4f1a\u88ab\u6253\u65ad\u3002<\/p>\n\n\n\n<p><strong>2\u3001lock_guard<\/strong><\/p>\n\n\n\n<p>\u4e00\u4e2a\u7528\u7c7b\u5b9e\u73b0\u7684\uff0c\u5305\u88c5\u597d\u4e86\u7684\u9501<br>\u58f0\u660e\u4e00\u4e2amutex\u53d8\u91cfa\u540e\uff0c\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u7c7b\u6a21\u677f\uff0c\u4f8b\uff1alock_guard&lt;mutex&gt; obj(a);obj\u5bf9\u8c61\u5728\u88ab\u521d\u59cb\u5316\u7684\u65f6\u5019\u81ea\u52a8\u52a0\u9501\uff0c\u80fd\u5728\u79bb\u5f00\u5f53\u524d\u4f5c\u7528\u57df\u540e\uff0c<strong>\u81ea\u52a8\u6790\u6784\u89e3\u9501<\/strong>\u3002<\/p>\n\n\n\n<p><strong>3\u3001unique_lock<\/strong><\/p>\n\n\n\n<p>\u4e5f\u662f\u4e00\u4e2a\u7528\u7c7b\u5b9e\u73b0\u7684\uff0c\u5305\u88c5\u597d\u7684\u9501\uff0c\u4f46\u76f8<strong>\u6bd4lock_guard \u529f\u80fd\u66f4\u591a\u66f4\u7075\u6d3b<\/strong>\uff0c\u6ca1\u6709\u989d\u5916\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u6548\u679c\u548clock_guard\u76f8\u540c\u3002\uff08unique_lock &lt;mutex&gt;  obj(a);\uff09<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u53c2\u6570\u53ef\u4ee5\u662f\uff1a <br> <strong>\u2460adopt_lock<\/strong>\uff08\u8868\u793a\u4e92\u65a5\u91cf\u5df2\u88ablock\uff0c\u65e0\u9700\u518d\u6b21\u52a0\u9501\uff0c\u5c31\u662f\u8bf4\u5728\u7528\u4e4b\u524d\u8fd9\u4e2a\u9501\u4e00\u5b9a\u662f\u5df2\u7ecf\u88ab\u9501\u4e86\u7684\uff0c\u8fd9\u4e2a\u53c2\u6570lock_guard\u4e5f\u662f\u53ef\u4ee5\u7528\u7684\uff09<br> <strong>\u2461try_to_lock<\/strong>\uff08\u5c1d\u8bd5\u53bb\u9501\uff0c\u5982\u679c\u6ca1\u9501\u6210\u529f\u4e5f\u4f1a\u8fd4\u56de\uff0c\u4e0d\u4f1a\u5361\u6b7b\u5728\u90a3\uff0c\u7136\u540e\u53ef\u7528owns_lock()\u5f97\u5230\u662f\u5426\u4e0a\u9501\u7684\u4fe1\u606f\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unique_lock&lt;mutex> obj(mute,try_to_lock);\nif (obj.owns_lock()) \n    {\/*\u5982\u679c\u9501\u4e0a\u4e86\u8981\u600e\u4e48\u505a\u2026*\/}\nelse \n    {\/*\u6ca1\u9501\u4e0a\u4e5f\u53ef\u4ee5\u5e72\u522b\u7684\u4e8b*\/}<\/code><\/pre>\n\n\n\n<p><strong>\u2462defer_lock<\/strong>\uff08\u7528\u4e00\u4e2a\u8fd8\u6ca1\u4e0a\u9501\u7684mutex\u53d8\u91cf\u521d\u59cb\u5316\u4e00\u4e2a\u5bf9\u8c61\uff0c\u81ea\u5df1\u53ef\u4ee5\u5728\u540e\u7eed\u4ee3\u7801\u6bb5\u4e2d\u7684\u67d0\u4e2a\u4f4d\u7f6e\u52a0\u9501\uff0c\u800c\u79bb\u5f00\u4f5c\u7528\u57df\u65f6\uff0c\u4e5f\u80fd\u5e2e\u52a9\u6211\u4eec\u89e3\u9501\uff0c\u5f53\u7136\u6211\u4eec\u4e5f\u80fd\u63d0\u524d\u624b\u52a8a.unlock()\u89e3\u9501\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        mutex a;\n\tunique_lock&lt;mutex> obj(a,defer_lock);\n\t\/* \u4e00\u4e9b\u4ee3\u7801 *\/\n\ta.lock();\/\/\u4e5f\u53ef\u7ed3\u5408\u6761\u4ef6\u5224\u65ad\u8bed\u53e5\u4f7f\u7528a.try_lock() \uff0c\u82e5\u6210\u529f\u9501\u4e0a\u80fd\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse \n<\/code><\/pre>\n\n\n\n<p>unique_lock\u8fd8\u6709\u4e00\u4e2a\u6210\u5458\u51fd\u6570release()\uff0c\u53ef\u8fd4\u56de\u4ed6\u6240\u7ba1\u7406\u7684mutex\u5bf9\u8c61\u6307\u9488\uff0c\u5e76\u91ca\u653e\u6240\u6709\u6743<\/p>\n\n\n\n<p>\u4e00\u822c\u6765\u8bb2\uff0c\u9501\u4f4f\u7684\u4ee3\u7801\u8d8a\u5c11\uff0c\u6548\u7387\u8d8a\u9ad8<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4f7f\u7528\u591a\u4e2a\u4e92\u65a5\u9501\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\uff1a\u6b7b\u9501<\/strong><\/p>\n\n\n\n<p>\u6709\u4e24\u4e2a\u7ebf\u7a0b\uff08A\u548cB\uff09\uff0c\u6709\u4e24\u4e2a\u9501\uff08c\u548cd\uff09\uff0cA\u9501\u4e86c\uff0c\u8fd8\u60f3\u8981d\u7684\u9501\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\uff0c\u4f46\u8fd9\u65f6B\u9501\u4e86d\uff0c\u4f46\u662f\u60f3\u8981c\u8fdb\u884c\u4e0b\u4e00\u6b65\u64cd\u4f5c\u3002\u4e8e\u662f\u5f7c\u6b64\u4e92\u76f8\u9501\u6b7b\u3002<\/p>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u6b7b\u9501\u7684\u4e00\u822c\u907f\u514d\u65b9\u6848\uff1a<\/strong><\/p>\n\n\n\n<p>1\u3001\u4fdd\u8bc1\u4e24\u4e2a\u4e92\u65a5\u9501\u7684<strong>\u4e0a\u9501\u987a\u5e8f\u4e00\u81f4<\/strong><br>2\u3001<strong>\u6216\u7528lock()\u8fd9\u4e2a\u51fd\u6570\u6a21\u677f\uff0c\u8fdb\u884c\u540c\u65f6\u4e0a\u9501<\/strong>\u3002\uff08\u53ea\u6709\u5f53\u6bcf\u4e2a\u9501\u90fd\u662f\u53ef\u9501\u7684\u72b6\u6001\uff0c\u624d\u4f1a\u771f\u6b63\u4e00\u6b21\u6027\u4e0a\u9501\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f8b\uff1a\n\tmutex a;\n\tmutex b;\n\tlock(a, b);\n\t\/*  ...  *\/\n\ta.unlock();\n\tb.unlock();\n\t\n\/*\u4e5f\u53ef\u5728lock(a,b)\u540e\u7528\uff0c\u4ee5\u7701\u53bb\u89e3\u9501\u6b65\u9aa4\uff08adopt_lock\u53c2\u6570\u8868\u793a\uff0c\u8be5\u9501\u5df2\u9501\uff0c\u4e0d\u91cd\u590d\u4e0a\u9501\uff0c\u53ea\u5728\u6790\u6784\u65f6\uff0c\u6267\u884c\u89e3\u9501\uff09\n\tlock_guard&lt;mutex> obj1(a,adopt_lock);\n\tlock_guard&lt;mutex> obj2(b, adopt_lock);\n*\/\n<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>B.\u539f\u5b50\u64cd\u4f5c<\/strong><\/p>\n\n\n\n<p>\u4fdd\u8bc1<strong>\u5bf9\u67d0\u4e2a\u53d8\u91cf\u8fdb\u884c\u4e00\u5143\u64cd\u4f5c\u7b26\u8fd0\u7b97\u7684\u65f6\u5019\uff0c\u80fd\u591f\u4e0d\u88ab\u6253\u65ad<\/strong>\uff0c\u53ea\u9700\u5c06\u8be5\u53d8\u91cf\u901a\u8fc7atomic\u8fd9\u4e2a\u7c7b\u6a21\u677f\u58f0\u660e\u5373\u53ef\uff0c\u6548\u7387\u6bd4\u4e92\u65a5\u9501\u9ad8<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class text_class\n{\npublic:\n\tatomic&lt;int> count;\n\ttext_class():count(0){}\n\tvoid WriteAval()\n\t{\n\t\tfor (int i = 0; i &lt; 100000; ++i)\n\t\t{\n\t\t\t++(count);\n\t\t}\n\t}\n};\n\/\/main\u51fd\u6570\u91cc\n\ttext_class B;\n\tthread th1(&amp;text_class::WriteAval, ref(B));\n\tthread th2(&amp;text_class::WriteAval, ref(B));\n\tth1.join();\n\tth2.join();\n\tcout &lt;&lt; B.count &lt;&lt; endl;<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u5e26\u5176\u5b83\u529f\u80fd\u7684\u4e92\u65a5\u9501<\/strong><\/p>\n\n\n\n<p><strong>1\u3001 recursive_mutex \uff08\u53ef\u91cd\u590d\u52a0\u9501\u7684\u4e92\u65a5\u91cf\uff09<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u67d0\u4e2a\u7ebf\u7a0b\u9700\u8981\u5bf9\u540c\u4e00\u4e2a\u9501\u8fdb\u884c\u591a\u6b21\u52a0\u9501\uff0c\u90a3\u4e48\u53ef\u4ee5\u7528recursive_mutex\u4ee3\u66ffmutex\u53bb\u58f0\u660e\u4e92\u65a5\u91cf\uff0c\u52a0\u4e86\u591a\u5c11\u6b21\uff0c\u8fd8\u662f\u5f97\u89e3\u9501\u591a\u5c11\u6b21<\/p>\n\n\n\n<p><strong>2\u3001timed_mutex\uff08 \u5e26\u8d85\u65f6\u7684\u4e92\u65a5\u91cf \uff09<\/strong><\/p>\n\n\n\n<p>\u2460\u7528<strong>try_lock_for<\/strong>\u6210\u5458\u51fd\u6570\uff0c\u53c2\u6570\u662f\u7b49\u5f85\u7684\u65f6\u95f4\uff0c\u7b49\u4e00\u6bb5\u65f6\u95f4\uff0c\u82e5\u6210\u529f\u62ff\u5230\u5c31\u9501\u4e0a\u5e76\u8fd4\u56detrue\uff0c\u53cd\u4e4b\u8fd4\u56defalse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>timed_mutex a;\n\tchrono::microseconds timeout(100);\n\tif (a.try_lock_for(timeout))\/\/\u5982\u679c\u5728\u89c4\u5b9a\u65f6\u95f4\u5185\u62ff\u5230\u4e86\u9501\n\t{\n\t\t\/*  \u4e00\u6ce2\u64cd\u4f5c *\/\n\t\ta.unlock();\/\/\u89e3\u9501\n\t}\n\telse\n\t{\n\t\t\/\/\u6ca1\u62ff\u5230\u9501\n\t}<\/code><\/pre>\n\n\n\n<p>\u2461\u7528<strong>try_lock_until<\/strong>\u6210\u5458\u51fd\u6570\uff0c\u53c2\u6570\u662f\u65f6\u95f4\u70b9\uff0c\u8981\u662f\u5230\u4e86\u8fd9\u4e2a\u65f6\u95f4\u70b9\uff0c\u6210\u529f\u62ff\u5230\u9501\uff0c\u5c31\u9501\u4e0a\u5e76\u8fd4\u56detrue\uff0c\u6ca1\u62ff\u5230\u8fd4\u56defalse<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>timed_mutex a;\n\tchrono::microseconds timeout(100);\n\tif (a.try_lock_until(chrono::steady_clock::now()+timeout))\/\/\u5982\u679c\u5728\u89c4\u5b9a\u65f6\u95f4\u5185\u62ff\u5230\u4e86\u9501\n\t{\n\t\t\/*  \u4e00\u6ce2\u64cd\u4f5c *\/\n\t\ta.unlock();\/\/\u89e3\u9501\n\t}\n\telse\n\t{\n\t\t\/\/\u6ca1\u62ff\u5230\u9501\n\t}<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u6761\u4ef6\u53d8\u91cfcondition_variable,wait(),notify_one()<\/strong><\/p>\n\n\n\n<p>A,B\u4e24\u4e2a\u7ebf\u7a0b<br>A\u7ebf\u7a0b\u5f80\u4e0b\u6267\u884c\u662f\u9700\u8981\u6761\u4ef6\u7684\uff0c<br>B\u7ebf\u7a0b\u53ef\u4ee5\u63d0\u4f9b\u4e00\u4e2a\u8fd9\u6837\u7684\u6761\u4ef6(\u4e0d\u4e00\u5b9a\u662f\u4e00\u5bf9\u4e00\u7684\u5173\u7cfb\uff0c\u4e5f\u53ef\u80fdB\u6267\u884c\u4e86\u4e00\u6b21\uff0cA\u5c31\u80fd\u62ff\u53bb\u6267\u884c\u597d\u51e0\u6b21\u4e86\uff0c\u4e5f\u53ef\u80fd\u9700\u8981B\u6267\u884c\u597d\u51e0\u6b21\u540e\uff0cA\u624d\u6ee1\u8db3\u6761\u4ef6\u6267\u884c\u4e00\u6b21)<br>\u90a3\u4e48\u5f53A\u4e0d\u6ee1\u8db3\u6761\u4ef6\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u5e94\u8be5\u518d\u8dd1A\u7ebf\u7a0b\uff08\u6d6a\u8d39\u8d44\u6e90\uff09\uff0c\u800c\u5e94\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\uff0c\u8ba9B\u53bb\u8dd1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Data_\n{\n\tqueue&lt;int> MsgQueue;\n\tmutex mute;\n\tcondition_variable my_con;\npublic:\n\tvoid GetMsg()\n\t{\n\t\tfor (int i = 0; i &lt; 10000; ++i)\n\t\t{\n\t\t\tunique_lock&lt;mutex> obj(mute);\n\t\t\tmy_con.wait(obj, &#91;this]\n\t\t\t{\n\t\t\t\tif (MsgQueue.empty())\n\t\t\t\t\treturn false;\/\/\u5982\u679c\u7a7a\u4e86\u7684\u8bdd\u5c31\u76f4\u63a5\u7b49\u5f85\uff0c\u5e76\u4e14\u89e3\u9501\uff0c\u8fd9\n                            \/\/\u4e2a\u7ebf\u7a0b\u5c31\u4e0d\u8981\u518d\u8dd1\u4e86\uff0c\u7b49\u5230\u88ab\u5524\u9192\u65f6\uff0c\u518d\u91cd\u65b0\u52a0\u9501\uff0c\u5f80\u4e0b\u6267\u884c\n\t\t\t\treturn true;\/\/\u5982\u679c\u6ca1\u7a7a\uff0c\u90a3\u5c31\u8bfb\u51fa\uff0c\u7ee7\u7eed\u8be5\u8fdb\u7a0b\n\t\t});\n\t\tcout &lt;&lt; \"\u8bfb\u51fa\" &lt;&lt; MsgQueue.front() &lt;&lt; endl;\/\/\u5230\u4e86\u8fd9\u91cc\u5c31\u8bc1\u660e\uff0c\u961f\u5217\u6ca1\u7a7a\uff0c\u53ef\u8bfb\u6d88\u606f\n\t\tMsgQueue.pop();\n\t\t}\n\t}\n\tvoid SaveMsh()\n\t{\n\t\tfor (int i = 0; i &lt; 10000; ++i)\n\t\t{\n\t\t\tunique_lock&lt;mutex> obj(mute);\n\t\t\tMsgQueue.push(i);\n\t\t\tcout &lt;&lt; \"\u88c5\u5165\" &lt;&lt; i &lt;&lt; endl;\n\t\t\tmy_con.notify_one();\/\/\u6211\u5df2\u7ecf\u52a0\u5165\u5143\u7d20\uff0c\u53ef\u4ee5\u5f00\u59cb\u516c\u5e73\u7ade\u4e89\uff08\u5524\u9192\uff09\n\t\t}\/\/\u4e0d\u4e00\u5b9a\u6bcf\u6b21notify_one()\u65f6\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u90fd\u5728\u7b49\u5f85\uff0c\u4e5f\u53ef\u80fd\u4eba\u5bb6\u5728\u5e72\u522b\u7684\u4e8b\n\t}\n};\n\/\/main\u51fd\u6570\u91cc\nData_ var;\n\tthread obj1(&amp;Data_::GetMsg, &amp;var);\n\tthread obj2(&amp;Data_::SaveMsh, &amp;var);\n\tobj1.join();\n\tobj2.join()<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u5141\u8bb8\u5524\u9192\u591a\u4e2a\u7ebf\u7a0b\u7684\u8bdd\uff0c\u53ef\u4ee5\u7528notify_all()<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p style=\"font-size:24px\">\u4e8c\u3001<strong>\u5176\u5b83<\/strong><\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u4f7f\u7528\u5355\u4f8b\u8bbe\u8ba1\u6a21\u5f0f\u7684\u65f6\u5019\uff0c\u5982\u679c\u6211\u4eec\u628a\u5355\u4f8b\u7c7b\u7684\u53d8\u91cf\u7684\u521d\u59cb\u5316\u653e\u5728\u4e86\u7ebf\u7a0b\u91cc\u9762\uff08\u4e00\u822c\u4e0d\u63a8\u8350\u8fd9\u6837\u505a\uff09\uff0c\u6211\u4eec\u5c31\u9700\u8981<strong>\u786e\u4fdd\u8fd9\u4e2a\u521d\u59cb\u5316\u53ea\u4f1a\u6267\u884c\u4e00\u6b21<\/strong>\uff0c\u5b9e\u73b0\u7684\u624b\u6bb5\u6709\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<p>\u2460\u53cc\u91cd\u6761\u4ef6\u5224\u65ad\uff0c\u5728\u9ad8\u6548\u7684\u540c\u65f6\uff0c\u786e\u4fdd\u521d\u59cb\u5316\u53ea\u80fd\u88ab\u6267\u884c\u4e00\u6b21<br> \u6bd4\u5982\u8bf4\u8fd4\u56de\u5355\u7c7b\u4e2d\u67d0\u4e2a\u6307\u9488\uff0c\u5148\u5224\u65ad\u662f\u5426\u8be5\u6307\u9488\u4e3a\u7a7a\uff0c\u82e5\u4e3a\u7a7a\uff0c\u5219\u5148\u52a0\u9501\uff0c\u518d\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5c31\u521d\u59cb\u5316\u8be5\u6307\u9488\u3002\u5982\u679c\u4e0d\u4e3a\u7a7a\u5c31\u8fd4\u56de\u6b64\u65f6\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u2461\u4f7f\u7528call_once()\uff0c\u80fd\u4fdd\u8bc1\u8be5\u51fd\u6570\u53ea\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        once_flag g_flag;\/\/\u5f53\u7136\u8fd9\u4e2a\u662f\u653e\u5728\u5927\u5bb6\u90fd\u80fd\u8bbf\u95ee\u5230\u7684\u5730\u65b9\n        \/*  ...   *\/\n\tcall_once(g_flag,\u51fd\u6570\u540d )<\/code><\/pre>\n\n\n\n<p><strong>\u6d45\u8c08\u7ebf\u7a0b\u6c60\uff1a<\/strong><br><strong>\u5f53\u7ebf\u7a0b\u6570\u7531\u8bf7\u6c42\u6240\u51b3\u5b9a\uff0c\u5c31\u4e0d\u80fd\u7b80\u5355\u5730\u6839\u636e\u8bf7\u6c42\u800c\u521b\u5efa\u7ebf\u7a0b<\/strong>\u3002\u5e94\u8be5\u8981\u7a0b\u5e8f\u542f\u52a8\u7684\u65f6\u5019\u5c31\u628a\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u521b\u5efa\u597d\uff0c\u653e\u5728\u6c60\u5b50\u91cc\uff0c\u9700\u8981\u7528\u7684\u65f6\u5019\u5c31\u62ff\u4e00\u4e2a\uff0c\u7528\u5b8c\u653e\u56de\u53bb\uff0c\u4ee5\u4fbf\u4e0b\u6b21\u8c03\u7528\uff0c\u8fd9\u79cd\u5faa\u73af\u5229\u7528\u7ebf\u7a0b\u7684\u65b9\u5f0f\u5c31\u662f\u7ebf\u7a0b\u6c60<br> \u4e00\u822c\u6765\u8bb2\u7ebf\u7a0b\u7684\u521b\u5efa\u6570\u91cf\uff0c\u4e24\u5343\u5c31\u662f\u6781\u9650\uff0c\u4e00\u822c\u5efa\u8bae200~300\u4e2a\uff0c\u4f46\u5177\u4f53\u60c5\u51b5\u5e94\u8be5\u5177\u4f53\u5206\u6790\uff0c\u5982\u679c\u8981\u8c03\u7528\u67d0\u4e9bapi\uff0c\u5e76\u4e14api\u6709\u63a8\u8350\u4f7f\u7528\u7684\u7ebf\u7a0b\u6570\u91cf\uff0c\u5c31\u5e94\u8be5\u6839\u636e\u5b83\u6765\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5171\u4eab\u6570\u636e\u95ee\u9898 1\u3001\u591a\u4e2a\u7ebf\u7a0b\u53ea\u8bfb\uff0c\u662f\u5b89\u5168\u7a33\u5b9a\u76842\u3001\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5199\uff0c\u6216\u8005\u65e2\u6709\u8bfb\u7ebf\u7a0b\uff0c\u4e5f\u6709\u5199\u7ebf\u7a0b\uff0c\u82e5\u4e0d\u52a0\u5904\u7406\uff0c\u5c31&hellip;<a href=\"https:\/\/www.notown.top\/?p=463\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">C++\u591a\u7ebf\u7a0b\uff08\u7b14\u8bb0\u4e8c\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14],"tags":[],"class_list":["post-463","post","type-post","status-publish","format-standard","hentry","category-14"],"_links":{"self":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=463"}],"version-history":[{"count":9,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/463\/revisions"}],"predecessor-version":[{"id":472,"href":"https:\/\/www.notown.top\/index.php?rest_route=\/wp\/v2\/posts\/463\/revisions\/472"}],"wp:attachment":[{"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.notown.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}