{"id":4533,"date":"2020-08-26T14:52:20","date_gmt":"2020-08-26T06:52:20","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=4533"},"modified":"2020-08-26T14:52:20","modified_gmt":"2020-08-26T06:52:20","slug":"%e5%85%b1%e4%ba%ab%e5%86%85%e5%ad%98%e6%97%a0%e9%94%81%e9%98%9f%e5%88%97%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/4533","title":{"rendered":"\u5171\u4eab\u5185\u5b58\u65e0\u9501\u961f\u5217\u7684\u5b9e\u73b0"},"content":{"rendered":"<div class=\"col-2-article-header\">\n<h1 class=\"article-title J-articleTitle\"><a href=\"https:\/\/cloud.tencent.com\/developer\/article\/1006241\">\u539f\u6587\u5730\u5740<\/a><\/h1>\n<p>&nbsp;<\/p>\n<div class=\"article-infos-wrap\">\n<div class=\"article-infos\"><span class=\"article-info\"><time title=\"2017-11-06 10:50:54\" datetime=\"2017-11-06 10:50:54\">2017-11-06<\/time><\/span><span class=\"article-info\">\u9605\u8bfb\u00a05.2K<\/span>0<\/div>\n<div class=\"article-infos-extra\"><\/div>\n<\/div>\n<\/div>\n<div class=\"com-markdown-collpase\">\n<div class=\"com-markdown-collpase-main\">\n<div class=\"rno-markdown J-articleContent\">\n<p>\u4f5c\u8005\uff1a\u8303\u5065<\/p>\n<p><strong>\u5bfc\u8bed\uff1a<\/strong> \u5171\u4eab\u5185\u5b58\u65e0\u9501\u961f\u5217\u662f\u8001\u8c03\u91cd\u5f39\u4e86\uff0c\u76f8\u5173\u7684\u5b9e\u73b0\u7f51\u4e0a\u90fd\u80fd\u627e\u5230\u5f88\u591a\u3002\u4f46\u770b\u4e86\u516c\u53f8\u5185\u5916\u7684\u5f88\u591a\u5b9e\u73b0\uff0c\u90fd\u6709\u4e0d\u5c11\u7684\u95ee\u9898\uff0c\u4e8e\u662f\u81ea\u5df1\u505a\u4e86\u91cd\u65b0\u5b9e\u73b0\u3002\u4e3b\u8981\u662f\u8003\u8651\u4e86\u4e00\u4e9b\u5f02\u5e38\u60c5\u51b5\u52a0\u5f3a\u5065\u58ee\u6027\uff0c\u5e76\u4e14\u8003\u8651\u4e86C++11\u7684\u5185\u5b58\u6a21\u578b\u3002<\/p>\n<h2 id=\"%E4%B8%BA%E4%BB%80%E4%B9%88%E9%9C%80%E8%A6%81%E5%85%B1%E4%BA%AB%E5%86%85%E5%AD%98%E6%97%A0%E9%94%81%E9%98%9F%E5%88%97%EF%BC%9F\">\u4e3a\u4ec0\u4e48\u9700\u8981\u5171\u4eab\u5185\u5b58\u65e0\u9501\u961f\u5217\uff1f<\/h2>\n<p>&nbsp;<\/p>\n<p>\u4e3a\u4e86\u4fbf\u4e8e\u67e5\u627e\u5b9a\u4f4d\u95ee\u9898\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u65e5\u5fd7\u6536\u96c6\u8ddf\u8e2a\u7cfb\u7edf\uff0c\u6bcf\u4e2a\u4e1a\u52a1\u6a21\u5757\u90fd\u9700\u8981\u8c03\u7528SDK\u8f93\u51fa\u683c\u5f0f\u5316\u7684\u672c\u5730\u65e5\u5fd7\u5e76\u5c06\u65e5\u5fd7\u53d1\u9001\u5230\u8fdc\u7aef\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u53d1\u9001\u65e5\u5fd7\u963b\u585e\u4e1a\u52a1\uff0c\u5178\u578b\u7684\u505a\u6cd5\u662f\u4e1a\u52a1\u7ebf\u7a0b\u5c06\u65e5\u5fd7\u5199\u5165\u961f\u5217\uff0c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5f02\u6b65\u5730\u4ece\u961f\u5217\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u53d1\u9001\u3002\u8003\u8651\u5230IO\u6027\u80fd\uff0c\u4e14\u65e5\u5fd7\u6570\u636e\u80fd\u5bb9\u5fcd\u5c0f\u6982\u7387\u7684\u4e22\u5931\uff0c\u6240\u4ee5\u961f\u5217\u4e0d\u5e94\u8be5\u662f\u5728\u78c1\u76d8\u4e0a\u3002\u53c8\u56e0\u4e3a\u4e1a\u52a1\u6a21\u5757\u53ef\u80fd\u662f\u591a\u7ebf\u7a0b\u6a21\u5f0f\u4e5f\u53ef\u80fd\u662f\u591a\u8fdb\u7a0b\u6a21\u5f0f\uff0c\u6240\u4ee5\u961f\u5217\u5e94\u8be5\u662f\u5728\u5171\u4eab\u5185\u5b58\u4e2d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u7b80\u5355\u7684\u505a\u6cd5\u662f\uff0c\u5bf9\u961f\u5217\u7684\u8bfb\u5199\u90fd\u52a0\u9501\uff0c\u4f46\u8fd9\u6837\u65e0\u7591\u4f1a\u5bfc\u81f4\u9ad8\u5e76\u53d1\u4e0b\u6027\u80fd\u74f6\u9888\u5c31\u5728\u8fd9\u628a\u9501\u4e0a\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u65e0\u9501\u961f\u5217\u3002\u770b\u4e86\u516c\u53f8\u5185\u5916\u5f88\u591a\u7248\u672c\u7684\u65e0\u9501\u961f\u5217\u5b9e\u73b0\uff0c\u591a\u591a\u5c11\u5c11\u90fd\u6709\u4e9b\u95ee\u9898\uff0c\u6240\u4ee5\u81ea\u5df1\u91cd\u65b0\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7248\u672c\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"%E7%8E%AF%E5%BD%A2%E6%95%B0%E7%BB%84\">\u73af\u5f62\u6570\u7ec4<\/h2>\n<p>&nbsp;<\/p>\n<p>\u5927\u90e8\u5206\u65e0\u9501\u961f\u5217\u90fd\u662f\u7528\u73af\u5f62\u6570\u7ec4\u5b9e\u73b0\u7684\uff0c\u7b80\u5355\u9ad8\u6548\uff0c\u8fd9\u91cc\u4e5f\u4e0d\u4f8b\u5916\u3002\u5047\u8bbe\u961f\u5217\u957f\u5ea6\u4e3aqueue_len\uff0c\u7528read_index\u8868\u793a\u53ef\u8bfb\u7684\u4f4d\u7f6e\uff0c\u7528write_index\u8868\u793a\u53ef\u5199\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u6bcf\u6b21\u4fee\u6539read_index\u6216write_index\u7684\u65f6\u5019\u90fd\u9700\u8981\u5c06\u5176\u5f52\u4e00\u5316\uff1a<\/p>\n<pre class=\"prism-token token  language-js\">read_index <span class=\"token operator\">%=<\/span> queue_len<\/pre>\n<p>\u961f\u5217\u5df2\u4f7f\u7528\u7a7a\u95f4used_len\u7684\u8ba1\u7b97\u4e3a\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prism-token token  language-js\">write_index <span class=\"token operator\">&gt;=<\/span> read_index <span class=\"token operator\">?<\/span>\r\n  write_index <span class=\"token operator\">-<\/span> read_index <span class=\"token punctuation\">:<\/span> queue_len <span class=\"token operator\">-<\/span> read_index <span class=\"token operator\">+<\/span> write_index<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5224\u65ad\u961f\u5217IsEmpty\u7684\u6761\u4ef6\u4e3a\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prism-token token  language-js\">read_index <span class=\"token operator\">==<\/span> write_index<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u4e0d\u505a\u7279\u6b8a\u5904\u7406\uff0c\u5224\u65ad\u961f\u5217IsFull\u7684\u6761\u4ef6\u548cIsEmpty\u7684\u6761\u4ef6\u4e00\u6837\uff0c\u4ece\u800c\u96be\u4ee5\u533a\u5206\u3002\u6240\u4ee5\u6211\u4eec\u5c06\u961f\u5217\u53ef\u5199\u5165\u957f\u5ea6\u8bbe\u4e3aqueue_len-1\u3002\u8fd9\u6837\u5224\u65ad\u957f\u5ea6\u4e3awrite_len\u7684\u6570\u636e\u662f\u5426\u53ef\u4ee5\u5199\u5165\u7684\u6761\u4ef6\u4e3a\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prism-token token  language-js\"><span class=\"token comment\">\/\/ \u6ce8\u610f\u662f &lt; \u800c\u4e0d\u662f &lt;= <\/span>\r\nused_len <span class=\"token operator\">+<\/span> write_len <span class=\"token operator\">&lt;<\/span> queue_len<\/pre>\n<p>&nbsp;<\/p>\n<h2 id=\"%E4%B8%80%E5%86%99%E4%B8%80%E8%AF%BB\">\u4e00\u5199\u4e00\u8bfb<\/h2>\n<p>&nbsp;<\/p>\n<p>\u5148\u6765\u8003\u8651\u4e00\u5199\u4e00\u8bfb\u7684\u573a\u666f\uff0c\u5b9e\u73b0\u8d77\u6765\u6700\u7b80\u5355\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5199\u64cd\u4f5c\uff1a\u5148\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u5199\u5165\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u5219\u5148\u5199\u6570\u636e\uff0c\u5199\u5b8c\u6570\u636e\u540e\u518d\u4fee\u6539write_index\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8bfb\u64cd\u4f5c\uff1a\u5148\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u8bfb\u53d6used_len &gt; 0\uff0c\u5982\u679c\u53ef\u4ee5\uff0c\u5219\u5148\u8bfb\u6570\u636e\uff0c\u8bfb\u5b8c\u518d\u4fee\u6539read_index\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u56e0\u4e3aread_index\u548cwrite_index\u90fd\u53ea\u4f1a\u6709\u4e00\u4e2a\u5730\u65b9\u53bb\u5199\uff0c\u6240\u4ee5\u5176\u5b9e\u4e0d\u9700\u8981\u52a0\u9501\u4e5f\u4e0d\u9700\u8981\u539f\u5b50\u64cd\u4f5c\uff0c\u76f4\u63a5\u4fee\u6539\u5373\u53ef\u3002\u9700\u8981\u6ce8\u610f\u8bfb\u5199\u6570\u636e\u7684\u65f6\u5019\u90fd\u9700\u8981\u8003\u8651\u9047\u5230\u6570\u7ec4\u5c3e\u90e8\u7684\u60c5\u51b5\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"%E5%A4%9A%E5%86%99%E4%B8%80%E8%AF%BB\">\u591a\u5199\u4e00\u8bfb<\/h2>\n<p>&nbsp;<\/p>\n<p>\u518d\u6765\u8003\u8651\u590d\u6742\u4e9b\u7684\u591a\u5199\u4e00\u8bfb\u7684\u573a\u666f\u3002\u56e0\u4e3a\u591a\u4e2a\u751f\u4ea7\u8005\u90fd\u4f1a\u4fee\u6539write_index\uff0c\u6240\u4ee5\u5728\u4e0d\u52a0\u9501\u7684\u60c5\u51b5\u4e0b\u5fc5\u987b\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\uff0c\u7b14\u8005\u4f7f\u7528\u7684\u662fGCC\u5185\u7f6e\u539f\u5b50\u64cd\u4f5c\u51fd\u6570\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prism-token token  language-js\"><span class=\"token comment\">\/\/ __sync\u7cfb\u5217\u7684\u5185\u7f6e\u51fd\u6570\u5728C++11\u4e4b\u540e\u5df2\u7ecf\u8fc7\u65f6\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528<\/span>\r\n<span class=\"token comment\">\/\/ C++11\u7684std::atomic\u51fd\u6570\u5c31\u662f\u7528__atomic\u7cfb\u5217\u5185\u7f6e\u51fd\u6570\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u4e5f\u8003\u8651\u4e86C++11\u63d0\u51fa\u7684\u5185\u5b58\u6a21\u578b<\/span>\r\n<span class=\"token comment\">\/\/ \u8be5\u51fd\u6570\u5728*ptr == *expected\u7684\u65f6\u5019\uff0c\u5c06*ptr = desired\uff0c\u5e76\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\uff0c\u5e76\u5c06*expected = *ptr<\/span>\r\n<span class=\"token comment\">\/\/ \u6700\u540e\u4e24\u4e2a\u53c2\u6570\u5206\u522b\u8868\u793a\u4fee\u6539\u6210\u529f\u548c\u5931\u8d25\u65f6\u4f7f\u7528\u7684\u5185\u5b58\u6a21\u578b\uff0c\u540e\u9762\u4f1a\u8bb2<\/span>\r\nbool <span class=\"token function\">__atomic_compare_exchange_n<\/span> <span class=\"token punctuation\">(<\/span>type <span class=\"token operator\">*<\/span>ptr<span class=\"token punctuation\">,<\/span> type <span class=\"token operator\">*<\/span>expected<span class=\"token punctuation\">,<\/span> type desired<span class=\"token punctuation\">,<\/span> bool weak<span class=\"token punctuation\">,<\/span> int success_memorder<span class=\"token punctuation\">,<\/span> int failure_memorder<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u4e00\u79cd\u9519\u8bef\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u6709\u7684\u5b9e\u73b0\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\u5bf9write_index\u4f7f\u7528\u4e86\u591a\u6b21\u539f\u5b50\u64cd\u4f5c\uff0c\u6bd4\u5982\u5148\u539f\u5b50\u589e\u52a0write_index\uff0c\u518d\u5199\u5165\u6570\u636e\uff0c\u5982\u679c\u5199\u5165\u5931\u8d25\uff0c\u518d\u539f\u5b50\u51cf\u5c0fwrite_index\uff0c\u770b\u8d77\u6765\u6bcf\u6b21\u64cd\u4f5c\u90fd\u662f\u539f\u5b50\u7684\uff0c\u4f46\u591a\u4e2a\u539f\u5b50\u64cd\u4f5c\u8fde\u5728\u4e00\u8d77\u5c31\u4e0d\u662f\u539f\u5b50\u64cd\u4f5c\u4e86\uff0c\u6574\u4e2a\u5199\u5165\u8fc7\u7a0b\u4e2d\u5bf9write_index\u5e94\u5f53\u53ea\u6709\u4e00\u6b21\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5e38\u89c1\u7684\u9519\u8bef\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1 .\u5148\u8bfb\u53d6write_index\uff0c\u5224\u65ad\u65b0\u7684\u6570\u636e\u662f\u5426\u6709\u8db3\u591f\u7684\u7a7a\u95f4\u53ef\u4ee5\u5199\u5165\u3002<\/p>\n<p>1.1 \u5982\u679c\u6ca1\u6709\u8db3\u591f\u7a7a\u95f4\u5219\u8fd4\u56de\u961f\u5217\u6ee1\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>2 .\u5982\u679c\u6709\u8db3\u591f\u7684\u7a7a\u95f4\uff0c\u5219\u51c6\u5907\u5199\u5165\u3002<\/p>\n<p>2.1 \u4e00\u5199\u7684\u65f6\u5019\uff0c\u662f\u5148\u5199\u6570\u636e\u518d\u6539write_index\u3002\u591a\u5199\u7684\u65f6\u5019\u4e3a\u4e86\u907f\u514d\u540c\u65f6\u5199\u5230\u540c\u4e00\u7247\u5185\u5b58\uff0c\u9700\u8981\u5148\u7533\u8bf7\u7a7a\u95f4\u518d\u5199\u5165\u6570\u636e\u3002\u5373\u5148\u539f\u5b50\u589e\u52a0write_index\uff0c\u5982\u679c\u6210\u529f\uff0c\u518d\u5199\u5165\u6570\u636e\u3002<\/p>\n<p>2.2 \u4e3a\u4e86\u907f\u514d\u5728\u751f\u4ea7\u8005\u8fd8\u672a\u5199\u5b8c\u6570\u636e\u7684\u65f6\u5019\uff0c\u6d88\u8d39\u8005\u5c31\u5c1d\u8bd5\u8bfb\u53d6\uff0c\u6240\u4ee5\u9700\u8981\u4e2a\u540c\u6b65\u673a\u5236\u544a\u8bc9\u6d88\u8d39\u8005\u6570\u636e\u6b63\u5728\u5199\u5165\u4e2d\u3002\u6bd4\u5982\u5934\u90e8\u9884\u7559\u4e00\u4e2a\u5b57\u8282\uff0c\u521d\u59cb\u4e3a0\u8868\u793a\u6b63\u5728\u5199\u5165\uff0c\u5199\u5b8c\u6570\u636e\u540e\u518d\u6539\u4e3a1\u8868\u793a\u5199\u5165\u5b8c\u6210\u3002\u5934\u90e8\u4e2d\u4e00\u822c\u8fd8\u67092\u5b57\u8282\u8868\u793a\u6570\u636e\u957f\u5ea6\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>3 .\u6d88\u8d39\u8005\u53d1\u73b0used_len &gt; 0\u5373\u53ef\u5c1d\u8bd5\u8bfb\u53d6\u3002<\/p>\n<p>3.1 \u5982\u679c\u9996\u5b57\u8282\u4e3a0\uff0c\u8868\u793a\u6570\u636e\u6b63\u5728\u5199\u5165\uff0c\u7b49\u5f85\u3002<\/p>\n<p>3.2 \u5982\u679c\u9996\u5b57\u8282\u4e0d\u4e3a0\uff0c\u8868\u793a\u6570\u636e\u5df2\u5199\u5b8c\uff0c\u53ef\u4ee5\u8bfb\u53d6\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>4 .\u6d88\u8d39\u8005\u8bfb\u53d6\u6570\u636e\u540e\uff0c\u9700\u8981\u5c06read_index\u524d\u79fb\u5230\u5408\u9002\u7684\u4f4d\u7f6e\uff0c\u4e14\u56e0\u4e3a\u53ea\u6709\u4e00\u4e2a\u6d88\u8d39\u8005\uff0c\u8fd9\u91cc\u65e0\u9700\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u79cd\u5b9e\u73b0\u770b\u4f3cOK\uff0c\u5176\u5b9e\u4e5f\u6709\u95ee\u9898\u3002\u5982\u679c\u751f\u4ea7\u8005\u5728\u4fee\u6539write_index\u4e4b\u540e\uff0c\u5728\u4fee\u6539\u5934\u90e8\u9996\u5b57\u8282\u4e3a1\u4e4b\u524d\uff0c\u8fd9\u6bb5\u65f6\u95f4\u5185crash\u7684\u8bdd\uff0c\u5c31\u4f1a\u5bfc\u81f4\u6d88\u8d39\u8005\u6c38\u8fdc\u505c\u7559\u5728\u7b49\u5f85\u751f\u4ea7\u8005\u5199\u5b8c\u7684\u72b6\u6001\u4e0a\uff0c\u4e14\u8fd9\u4e2a\u72b6\u6001\u65e0\u6cd5\u81ea\u52a8\u6062\u590d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6211\u7684\u4f18\u5316\u4e00\uff1a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ol class=\"ol-level-0\">\n<li>\u6d88\u8d39\u8005\u53d1\u73b0\u5934\u90e8\u9996\u5b57\u8282\u4e3a0\uff0c\u5219\u7b49\u5f85\uff0c\u4f46\u6700\u591a\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4\u6bd4\u59825ms\u3002<\/li>\n<li>\u5728\u5199\u5165\u6570\u636e\u9650\u5236\u4e86\u6700\u5927\u957f\u5ea6\u7684\u524d\u63d0\u4e0b\uff0c\u4ee5\u73b0\u4ee3\u8ba1\u7b97\u673a\u7684\u901f\u5ea6\uff0c\u4ece\u4fee\u6539write_index\u7136\u540ecopy\u6570\u636e\u6700\u540e\u4fee\u6539\u5934\u90e8\u9996\u5b57\u8282\u4e3a1\uff0c\u8fd9\u6bb5\u65f6\u95f4\u662f\u975e\u5e38\u5feb\u7684\uff0c\u8fdc\u5c0f\u4e8e5ms\u3002<\/li>\n<li>\u5982\u679c\u7b49\u5f855ms\u540e\uff0c\u53d1\u73b0\u9996\u5b57\u8282\u8fd8\u662f0\uff0c\u5219\u8ba4\u4e3a\u8be5\u751f\u4ea7\u8005crash\u4e86\uff0c\u6839\u636e\u5934\u90e8\u4e2d\u7684\u957f\u5ea6\u4fe1\u606f\uff0c\u5411\u524d\u8df3\u8fc7\u8fd9\u4e2a\u975e\u6cd5\u6570\u636e\u5757\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>\u4f46\u5982\u679c\u751f\u4ea7\u8005\u8fd8\u6ca1\u6765\u5f97\u53ca\u5199\u5165\u6570\u636e\u957f\u5ea6\u5c31crash\u4e86\u5462\uff1f\u5c31\u60f3\u8df3\u8fc7\u975e\u6cd5\u6570\u636e\u5757\u4e5f\u4e0d\u77e5\u9053\u8be5\u8df3\u591a\u5c11\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6211\u7684\u4f18\u5316\u4e8c\uff1a<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1 .\u5c06\u961f\u5217\u5206\u6210N\u4e2a\u5b9a\u957fblock\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prism-token token  language-js\">struct Block <span class=\"token punctuation\">{<\/span>\r\n union <span class=\"token punctuation\">{<\/span>\r\n     struct <span class=\"token punctuation\">{<\/span>\r\n         bool m_used<span class=\"token punctuation\">;<\/span>\r\n         uint8_t m_blk_cnt<span class=\"token punctuation\">;<\/span>\r\n         uint8_t m_blk_idx<span class=\"token punctuation\">;<\/span>\r\n         uint16_t m_blk_len<span class=\"token punctuation\">;<\/span>\r\n     <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>\r\n     char m_head_reserved<span class=\"token punctuation\">[<\/span><span class=\"token number\">8<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>\r\n <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span>\r\n char m_data<span class=\"token punctuation\">[<\/span>kBlockDataSize<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span>\r\n\r\n bool <span class=\"token function\">CanUsed<\/span><span class=\"token punctuation\">(<\/span>uint8_t expected_blk_idx<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token punctuation\">{<\/span>\r\n     <span class=\"token keyword\">return<\/span> m_used <span class=\"token operator\">&amp;&amp;<\/span> expected_blk_idx <span class=\"token operator\">==<\/span> m_blk_idx\r\n         <span class=\"token operator\">&amp;&amp;<\/span> m_blk_cnt <span class=\"token operator\">&lt;=<\/span> kMaxBlockCount\r\n         <span class=\"token operator\">&amp;&amp;<\/span> m_blk_idx <span class=\"token operator\">&lt;<\/span> m_blk_cnt\r\n         <span class=\"token operator\">&amp;&amp;<\/span> m_blk_len <span class=\"token operator\">&lt;=<\/span> kBlockDataSize<span class=\"token punctuation\">;<\/span>\r\n <span class=\"token punctuation\">}<\/span>\r\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>2 .\u751f\u4ea7\u8005\u5199\u6570\u636e\u65f6\u5148\u8ba1\u7b97\u9700\u8981\u7684blk_cnt\uff0c\u518d\u539f\u5b50\u5730\u5c06write_index\u524d\u79fbblk_cnt\u3002\u5199\u6570\u636e\u7684\u65f6\u5019\u7b2c\u4e00\u4e2ablock\u6700\u540e\u5199\uff0c\u6bcf\u4e2ablock\u5185\u90e8\u4f9d\u7136\u662f\u6700\u540e\u5199\u5934\u90e8\u9996\u5b57\u8282m_used = true\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>3 .\u5f53\u7b49\u5f855ms\u540e\u53d1\u73b0m_used\u8fd8\u662ffalse\uff0c\u8ba4\u4e3a\u5199\u5165\u8005crash\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u4ee5block\u4e3a\u5355\u4f4d\u5411\u524d\u8df3\u8dc3\uff0c\u76f4\u5230\u8df3\u5230\u4e00\u4e2a\u5408\u6cd5block\u6216\u8005\u6ca1\u6709\u53ef\u4ee5\u8bfb\u53d6\u7684\u6570\u636e\u4e3a\u6b62\u3002\u5408\u6cd5block\u5224\u65ad\u6761\u4ef6\u4e3ablk.CanUsed(0)\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u8fd9\u6837\u5c31\u7b97\u751f\u4ea7\u8005\u5728\u4efb\u610f\u65f6\u523bcrash\uff0c\u6d88\u8d39\u8005\u90fd\u6709\u80fd\u529b\u81ea\u52a8\u6062\u590d\uff0c\u627e\u5230\u4e0b\u4e00\u4e2a\u5408\u6cd5block\u3002\u4f46\u5982\u679c\u6d88\u8d39\u8005\u5e76\u6ca1\u6709\u771f\u6b63crash\u53ea\u662f\u56e0\u4e3a\u67d0\u79cd\u795e\u79d8\u7684\u539f\u56e0\u5199\u5165\u592a\u6162\u8d85\u8fc7\u4e865ms\uff0c\u600e\u4e48\u529e\uff1f<\/p>\n<p>&nbsp;<\/p>\n<ol class=\"ol-level-0\">\n<li>\u9996\u5148\uff0c\u56e0\u4e3a\u6d88\u8d39\u8005\u5df2\u7ecf\u8df3\u8fc7\uff0c\u6240\u4ee5\u5b83\u8fd9\u6b21\u5199\u5165\u7684\u6570\u636e\u80af\u5b9a\u662f\u4e0d\u4f1a\u88ab\u6d88\u8d39\u4e86\uff0c\u5373\u6781\u5c0f\u6982\u7387\u4f1a\u9057\u6f0f\u6570\u636e\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u6211\u4eec\u8003\u8651\u66f4\u6781\u5c0f\u6982\u7387\u7684\u60c5\u51b5\uff0c\u53ea\u6709\u5f53\u751f\u4ea7\u8005\u6162\u5230\u961f\u5217\u5faa\u73af\u4e86\u5b8c\u6574\u4e00\u8f6e\uff0c\u5176\u5b83\u751f\u4ea7\u8005\u91cd\u65b0\u7533\u8bf7\u5230\u8fd9\u7247block\u51c6\u5907\u5199\u5165\uff0c\u624d\u4f1a\u4ea7\u751f\u6570\u636e\u810f\u5199\u3002<\/li>\n<li>\u518d\u6b21\uff0c\u5c31\u7b97\u771f\u7684\u51fa\u73b0\u6570\u636e\u810f\u5199\uff0c\u4e00\u822c\u5934\u90e8\u7684blk_cnt\u548cblk_idx\u7b49\u4fe1\u606f\u4e0d\u4f1a\u5bf9\u4e0d\u4e0a\uff0c\u6d88\u8d39\u8005\u6bcf\u6b21\u6d88\u8d39\u6570\u636e\u90fd\u4f1a\u901a\u8fc7CanUsed\u51fd\u6570\u68c0\u6d4b\uff0c\u68c0\u6d4b\u4e0d\u901a\u8fc7\u7684\u90fd\u4f1a\u8df3\u8fc7\u3002<\/li>\n<li>\u6700\u540e\uff0c\u5982\u679c\u8bf4\u975e\u8981\u8003\u8651\u6781\u7aef\u60c5\u51b5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u5934\u90e8\u4e2d\u518d\u52a0\u5165block_crc\u548ctotal_crc\u6765\u6821\u9a8c\u6570\u636e\u3002\u7b14\u8005\u8003\u8651\u5230\u65e5\u5fd7\u6570\u636e\u5bb9\u5fcd\u8fd9\u79cd\u6781\u5c0f\u6982\u7387\u7684\u9519\u4e71\uff0c\u6240\u4ee5\u7701\u7565\u4e86\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><strong>\u5185\u5b58\u6a21\u578b<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u770b\u4f3c\u5b8c\u7f8e\u4e86\uff0c\u771f\u7684\u5417\uff1f\u5176\u5b9e\u4e0d\u7136\u3002\u4ee5\u4e0a\u8fd8\u6ca1\u6709\u8003\u8651\u5185\u5b58\u6a21\u578b\u3002\u56e0\u4e3a\u7f16\u8bd1\u5668\u7684\u4f18\u5316\uff0c\u5b9e\u9645\u4ee3\u7801\u6267\u884c\u987a\u5e8f\u4e0d\u4e00\u5b9a\u662f\u4f60\u5199\u7684\u987a\u5e8f\u3002\u4e5f\u5c31\u662f\u8bf4\u867d\u7136\u6211\u4eec\u662f\u5148\u5199\u6570\u636e\u6700\u540e\u8bbe\u7f6em_used = true\uff0c\u4f46\u5b9e\u9645\u6267\u884c\u987a\u5e8f\u5e76\u4e0d\u4e00\u5b9a\u771f\u7684\u5982\u6b64\uff0c\u6709\u53ef\u80fd\u5148\u6267\u884c\u4e86m_used = true\uff0c\u518d\u6267\u884c\u6570\u636ecopy\uff0c\u8fd9\u5c31\u4e71\u5957\u4e86\u3002\u56e0\u6b64\u6211\u4eec\u9700\u8981\u6307\u5b9a\u5185\u5b58\u6a21\u578b\u3002\u5173\u4e8e\u5185\u5b58\u6a21\u578b\u63a8\u8350\u53c2\u9605\u6587\u7ae0<a href=\"http:\/\/blog.jobbole.com\/106516\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" data-from=\"10680\">http:\/\/blog.jobbole.com\/106516\/<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u751f\u4ea7\u8005\u5bf9\u4e8em_used\u7684\u4fee\u6539\uff0c\u5185\u5b58\u6a21\u578b\u5e94\u8be5\u4f7f\u7528release\u3002\u4fdd\u8bc1\u5728\u8fd9\u4e2a\u64cd\u4f5c\u4e4b\u524d\u7684memory accesses\u4e0d\u4f1a\u91cd\u6392\u5230\u8fd9\u4e2a\u64cd\u4f5c\u4e4b\u540e\u53bb\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u5411\u6d88\u8d39\u8005\u63d0\u524d\u91ca\u653e\u53ef\u7528\u4fe1\u53f7\u3002<\/p>\n<pre class=\"prism-token token  language-js\"><span class=\"token function\">__atomic_store_n<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>blk<span class=\"token punctuation\">.<\/span>m_used<span class=\"token punctuation\">,<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">,<\/span> __ATOMIC_RELEASE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>2 .\u6d88\u8d39\u8005\u5bf9\u4e8em_used\u7684\u8bfb\u53d6\uff0c\u5185\u5b58\u6a21\u578b\u5e94\u8be5\u4f7f\u7528acquire\u3002\u4fdd\u8bc1\u5728\u8fd9\u4e2a\u64cd\u4f5c\u4e4b\u540e\u7684memory accesses\u4e0d\u4f1a\u91cd\u6392\u5230\u8fd9\u4e2a\u64cd\u4f5c\u4e4b\u524d\u53bb\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u63d0\u524d\u8bfb\u5230\u751f\u4ea7\u8005\u8fd8\u672a\u5199\u5b8c\u7684\u6570\u636e\u3002<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prism-token token  language-js\"><span class=\"token function\">__atomic_load_n<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>m_used<span class=\"token punctuation\">,<\/span> __ATOMIC_ACQUIRE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>3 .\u5bf9write_index\u7684\u4fee\u6539\uff0c\u5373\u8c03\u7528<strong>atomic_compare_exchange_n\u51fd\u6570\uff0c\u6700\u540e\u4e24\u4e2a\u53c2\u6570\u5e94\u8be5\u90fd\u662f<\/strong>ATOMIC_RELAXED\uff0c\u5373\u5185\u5b58\u6a21\u5f0f\u4f7f\u7528relaxed\uff0c\u5373\u6ca1\u6709\u7ea6\u675f\u3002\u56e0\u4e3awrite_index\u53ea\u662f\u591a\u751f\u4ea7\u8005\u4e4b\u95f4\u7528\u6765\u505a\u7c7b\u4f3c\u4e92\u65a5\u7684\u7ade\u4e89\uff0c\u672c\u6765\u5c31\u662f\u9760m_used\u771f\u6b63\u7ea6\u675f\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u4e4b\u95f4\u7684\u884c\u4e3a\u987a\u5e8f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5171\u4eab\u5185\u5b58<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u53e6\u5916\u4e00\u4e2a\u503c\u5f97\u4e00\u63d0\u7684\u70b9\u662f\uff0c\u5171\u4eab\u5185\u5b58\u6211\u4f7f\u7528mmap\uff0c\u800c\u975eshmget\u3002\u56e0\u4e3a\u62c5\u5fc3\u4e00\u53f0\u673a\u5668\u4e0a\u90e8\u7f72\u7684\u7a0b\u5e8f\u592a\u591a\uff0c\u53ef\u80fd\u51fa\u73b0\u5171\u4eab\u5185\u5b58key\u51b2\u7a81\u7684\u60c5\u51b5\u3002\u4e07\u4e00\u51fa\u73b0\u5171\u4eab\u5185\u5b58\u51b2\u7a81\uff0c\u88ab\u522b\u7684\u7a0b\u5e8f\u5199\u574f\u4e86\uff0c\u5c31\u4f1a\u51fa\u73b0\u83ab\u540d\u5176\u5999\u7684\u60c5\u51b5\u3002\u6240\u4ee5\u4f7f\u7528mmap\u6307\u5b9a\u6a21\u5757\u76f8\u5173\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u5c31\u4e0d\u7528\u592a\u62c5\u5fc3\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2 id=\"%E9%9C%80%E8%A6%81%E5%A4%9A%E8%AF%BB%E5%90%97%EF%BC%9F\">\u9700\u8981\u591a\u8bfb\u5417\uff1f<\/h2>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u518d\u8fdb\u4e00\u6b65\u5b9e\u73b0\u591a\u5199\u591a\u8bfb\uff0c\u9700\u8981\u5bf9read_index\u4e5f\u8003\u8651\u539f\u5b50\u64cd\u4f5c\uff0c\u52a0\u4e0a\u7a0d\u663e\u590d\u6742\u7684block\u68c0\u67e5\u8df3\u8dc3\u903b\u8f91\uff0c\u5b9e\u73b0\u96be\u5ea6\u8f83\u9ad8\u3002\u4f46\u6211\u4eec\u9996\u5148\u8be5\u95ee\u4e00\u4e2a\u95ee\u9898\uff0c\u771f\u7684\u9700\u8981\u591a\u8bfb\u5417\uff1f<\/p>\n<p>&nbsp;<\/p>\n<p>\u6211\u8ba4\u4e3a\u662f\u4e0d\u9700\u8981\u7684\uff1a<\/p>\n<p>&nbsp;<\/p>\n<ol class=\"ol-level-0\">\n<li>\u9996\u5148\uff0c\u6d88\u8d39\u8005\u53ef\u4ee5\u6279\u91cf\u8bfb\u53d6\uff0c\u4e00\u6b21\u8bfb\u53d6\u8db3\u591f\u6216\u8005\u5168\u90e8\u7684\u53ef\u8bfb\u6570\u636e\u3002\u901a\u8fc7\u5bf9\u540e\u7eed\u4e1a\u52a1\u903b\u8f91\u7684\u4f18\u5316\uff0c\u4e00\u822c\u5355\u8bfb\u90fd\u80fd\u6ee1\u8db3\u6027\u80fd\u8981\u6c42\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u53ef\u4ee5\u4e00\u8bfb\u6279\u91cf\u8bfb\u53d6\u540e\u518d\u505a\u8fdb\u4e00\u6b65\u8fdb\u7a0b\u5185\u591a\u7ebf\u7a0b\u5206\u53d1\uff0c\u4f1a\u66f4\u52a0\u7b80\u5355\u3002<\/li>\n<li>\u518d\u6b21\uff0c\u5982\u679c\u5355\u8bfb\u771f\u7684\u4e0d\u80fd\u6ee1\u8db3\u6027\u80fd\u8981\u6c42\uff0c\u8bf4\u660e\u8bfb\u540e\u7684\u4e1a\u52a1\u903b\u8f91\u975e\u5e38\u91cd\uff0c\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\uff0c\u6027\u80fd\u74f6\u9888\u5c31\u80af\u5b9a\u4e0d\u4f1a\u662f\u961f\u5217\u8bfb\u53d6\u8fd9\u91cc\u4e86\uff0c\u90a3\u4e48\u7ed9\u8bfb\u52a0\u9501\u65e0\u7591\u662f\u66f4\u5408\u9002\u7684\u9009\u62e9\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h2 id=\"%E6%9C%89%E6%84%9F%E8%80%8C%E5%8F%91\">\u6709\u611f\u800c\u53d1<\/h2>\n<p>&nbsp;<\/p>\n<ol class=\"ol-level-0\">\n<li>\u8981\u5199\u51fa\u9ad8\u5065\u58ee\u6027\u7684\u4ee3\u7801\uff0c\u4e00\u5b9a\u8981\u65f6\u523b\u8bb0\u5f97\uff0c\u7a0b\u5e8f\u53ef\u80fd\u4f1a\u5728\u4f60\u7684\u4efb\u4f55\u4e00\u884c\u4ee3\u7801\u5904\u56e0\u4e3abug\u6216\u8005\u610f\u5916crash\uff0c\u4e0d\u8981\u60f3\u5f53\u7136\u4ee5\u4e3a\u6267\u884c\u4e86\u4e0a\u4e00\u884c\u4ee3\u7801\u5c31\u4e00\u5b9a\u4f1a\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801\u3002crash\u540e\u91cd\u542f\u662f\u5426\u80fd\u6b63\u5e38\u6062\u590d\uff1f<\/li>\n<li>\u5199\u591a\u7ebf\u7a0b\u591a\u8fdb\u7a0b\u76f8\u5173\u7684\u903b\u8f91\uff0c\u6d89\u53ca\u5230\u5e76\u53d1\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u8981\u8003\u8651\u4ed4\u7ec6\uff0c\u9700\u4e0d\u9700\u8981\u52a0\u9501\uff1f\u4e0d\u52a0\u9501\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff1f<\/li>\n<li>\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u7b49\u5171\u4eab\u8d44\u6e90\u65f6\uff0c\u66f4\u8981\u60f3\u5230\uff0c\u8fd9\u8d44\u6e90\u4e0d\u662f\u6211\u72ec\u5360\u7684\uff0c\u4e07\u4e00\u88ab\u6709\u610f\u6216\u65e0\u610f\u7684\u7be1\u6539\u4e86\u6570\u636e\u8be5\u600e\u4e48\u529e\uff1f\u80fd\u5426\u5c3d\u91cf\u907f\u514d\u88ab\u522b\u4eba\u7be1\u6539\uff1f\u5982\u679c\u88ab\u7be1\u6539\uff0c\u662f\u5426\u6709\u53d1\u73b0\u548c\u6062\u590d\u673a\u5236\uff1f<\/li>\n<li>\u4e0d\u8981\u4ee5\u4e3a\u4f60\u5199\u7684\u4ee3\u7801\u987a\u5e8f\u5c31\u662f\u771f\u6b63\u7684\u6267\u884c\u987a\u5e8f\uff0c\u9700\u8981\u8003\u8651\u5185\u5b58\u6a21\u578b\u3002<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; 2017-11-<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-4533","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=4533"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/4533\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=4533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=4533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=4533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}