{"id":15305,"date":"2024-11-17T20:10:15","date_gmt":"2024-11-17T12:10:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15305"},"modified":"2024-11-17T20:10:15","modified_gmt":"2024-11-17T12:10:15","slug":"%e9%94%81%e6%9c%ba%e5%88%b6%e5%8f%8acas%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15305\/","title":{"rendered":"\u9501\u673a\u5236\u53caCAS\u5b9e\u73b0\u539f\u7406"},"content":{"rendered":"<h3>\u4e00\u3001\u9501\u673a\u5236<\/h3>\n<p>\u5e38\u7528\u7684\u9501\u673a\u5236\u6709\u4e24\u79cd\uff1a\u60b2\u89c2\u9501\u3001\u4e50\u89c2\u9501\uff0c\u4e0b\u9762\u6211\u4eec\u5206\u522b\u4ecb\u7ecd\u4e0b\u3002<\/p>\n<h5>1\u3001\u60b2\u89c2\u9501<\/h5>\n<ul>\n<li>\u5047\u5b9a\u4f1a\u53d1\u751f\u5e76\u53d1\u51b2\u7a81\uff0c\u5c4f\u853d\u4e00\u5207\u53ef\u80fd\u8fdd\u53cd\u6570\u636e\u5b8c\u6574\u6027\u7684\u64cd\u4f5c\u3002<\/li>\n<li>\u60b2\u89c2\u9501\u7684\u5b9e\u73b0\uff0c\u5f80\u5f80\u4f9d\u9760\u5e95\u5c42\u63d0\u4f9b\u7684\u9501\u673a\u5236\u3002<\/li>\n<li>\u60b2\u89c2\u9501\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\uff0c\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u3002<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h5>2\u3001\u4e50\u89c2\u9501<\/h5>\n<ul>\n<li>\u5047\u8bbe\u4e0d\u4f1a\u53d1\u751f\u5e76\u53d1\u51b2\u7a81\uff0c\u6bcf\u6b21\u4e0d\u52a0\u9501\u800c\u662f\u5047\u8bbe\u6ca1\u6709\u51b2\u7a81\u800c\u53bb\u5b8c\u6210\u67d0\u9879\u64cd\u4f5c\uff0c\u53ea\u5728\u63d0\u4ea4\u64cd\u4f5c\u65f6\u68c0\u67e5\u662f\u5426\u8fdd\u53cd\u6570\u636e\u5b8c\u6574\u6027\u3002<\/li>\n<li>\u5982\u679c\u56e0\u4e3a\u51b2\u7a81\u5931\u8d25\u5c31\u91cd\u8bd5\uff0c\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002<\/li>\n<li>\u4e50\u89c2\u9501\u5927\u591a\u662f\u57fa\u4e8e\u6570\u636e\u7248\u672c\u8bb0\u5f55\u673a\u5236\u5b9e\u73b0\u3002<\/li>\n<li>\u4e3a\u6570\u636e\u589e\u52a0\u4e00\u4e2a\u7248\u672c\u6807\u8bc6\uff0c\u6bd4\u5982\u5728\u57fa\u4e8e\u6570\u636e\u5e93\u8868\u7684\u7248\u672c\u89e3\u51b3\u65b9\u6848\u4e2d\uff0c\u4e00\u822c\u662f\u901a\u8fc7\u4e3a\u6570\u636e\u5e93\u8868\u589e\u52a0\u4e00\u4e2a \u201cversion\u201d \u5b57\u6bb5\u6765\u5b9e\u73b0\u3002\u8bfb\u53d6\u51fa\u6570\u636e\u65f6\uff0c\u5c06\u6b64\u7248\u672c\u53f7\u4e00\u540c\u8bfb\u51fa\uff0c\u4e4b\u540e\u66f4\u65b0\u65f6\uff0c\u5bf9\u6b64\u7248\u672c\u53f7\u52a0\u4e00\u3002<\/li>\n<li>\u6b64\u65f6\uff0c\u5c06\u63d0\u4ea4\u6570\u636e\u7684\u7248\u672c\u6570\u636e\u4e0e\u6570\u636e\u5e93\u8868\u5bf9\u5e94\u8bb0\u5f55\u7684\u5f53\u524d\u7248\u672c\u4fe1\u606f\u8fdb\u884c\u6bd4\u5bf9\uff0c\u5982\u679c\u63d0\u4ea4\u7684\u6570\u636e\u7248\u672c\u53f7\u5927\u4e8e\u6570\u636e\u5e93\u8868\u5f53\u524d\u7248\u672c\u53f7\uff0c\u5219\u4e88\u4ee5\u66f4\u65b0\uff0c\u5426\u5219\u8ba4\u4e3a\u662f\u8fc7\u671f\u6570\u636e\u3002<\/li>\n<li>\u4e50\u89c2\u9501\u7684\u7f3a\u70b9\u662f\u4e0d\u80fd\u89e3\u51b3\u810f\u8bfb\u7684\u95ee\u9898\u3002<\/li>\n<li>\u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u91cc\u8fb9,\u5982\u679c\u5e76\u53d1\u91cf\u4e0d\u5927\u4e14\u4e0d\u5141\u8bb8\u810f\u8bfb\uff0c\u53ef\u4ee5\u4f7f\u7528\u60b2\u89c2\u9501\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002<\/li>\n<li>\u5982\u679c\u7cfb\u7edf\u7684\u5e76\u53d1\u975e\u5e38\u5927\u7684\u8bdd,\u60b2\u89c2\u9501\u5b9a\u4f1a\u5e26\u6765\u975e\u5e38\u5927\u7684\u6027\u80fd\u95ee\u9898,\u6240\u4ee5\u6211\u4eec\u5c31\u8981\u9009\u62e9\u4e50\u89c2\u9501\u5b9a\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h5>3\u3001\u9501\u673a\u5236\u5b58\u5728\u7684\u95ee\u9898<\/h5>\n<p>\u5728\u591a\u7ebf\u7a0b\u7ade\u4e89\u4e0b\uff0c\u52a0\u9501\u3001\u91ca\u653e\u9501\u4f1a\u5bfc\u81f4\u6bd4\u8f83\u591a\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u8c03\u5ea6\u5ef6\u65f6\uff0c\u5f15\u8d77\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<ul>\n<li>\u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u9501\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u6b64\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\u3002<\/li>\n<li>\u5982\u679c\u4e00\u4e2a\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u7b49\u5f85\u4e00\u4e2a\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u4f1a\u5bfc\u81f4\u4f18\u5148\u7ea7\u5012\u7f6e\uff0c\u5f15\u8d77\u6027\u80fd\u98ce\u9669\u3002<\/li>\n<\/ul>\n<p>volatile\u662f\u4e0d\u9519\u7684\u673a\u5236\uff0c\u4f46\u662fvolatile\u4e0d\u80fd\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u56e0\u6b64\u5bf9\u4e8e\u540c\u6b65\u6700\u7ec8\u8fd8\u662f\u8981\u56de\u5230\u9501\u673a\u5236\u4e0a\u6765\u3002<\/p>\n<h5>4\u3001\u4e24\u79cd\u9501\u603b\u7ed3<\/h5>\n<ul>\n<li>\u72ec\u5360\u9501\u662f\u4e00\u79cd\u60b2\u89c2\u9501\uff0csynchronized\u5c31\u662f\u4e00\u79cd\u72ec\u5360\u9501\uff0c\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\uff0c\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u3002<\/li>\n<li>\u6240\u8c13\u4e50\u89c2\u9501\u5c31\u662f\uff0c\u6bcf\u6b21\u4e0d\u52a0\u9501\u800c\u662f\u5047\u8bbe\u6ca1\u6709\u51b2\u7a81\u800c\u53bb\u5b8c\u6210\u67d0\u9879\u64cd\u4f5c\uff0c\u5982\u679c\u56e0\u4e3a\u51b2\u7a81\u5931\u8d25\u5c31\u91cd\u8bd5\uff0c\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002<\/li>\n<\/ul>\n<h3>\u4e8c\u3001CAS\uff08Compare &amp; Set\/Compare AND Swap\uff09<\/h3>\n<p>CAS\u662f\u89e3\u51b3\u591a\u7ebf\u7a0b\u5e76\u884c\u60c5\u51b5\u4e0b\u4f7f\u7528\u9501\u9020\u6210\u6027\u80fd\u635f\u8017\u7684\u4e00\u79cd\u673a\u5236\u3002<\/p>\n<ul>\n<li>CAS\u64cd\u4f5c\u5305\u542b\u4e09\u4e2a\u64cd\u4f5c\u6570\u2014\u2014\u5185\u5b58\u4f4d\u7f6e\uff08V\uff09\u3001\u9884\u671f\u539f\u503c\uff08A\uff09\u3001\u65b0\u503c(B)\u3002<\/li>\n<li>\u5982\u679c\u5185\u5b58\u4f4d\u7f6e\u7684\u503c\u4e0e\u9884\u671f\u539f\u503c\u76f8\u5339\u914d\uff0c\u90a3\u4e48\u5904\u7406\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u4f4d\u7f6e\u503c\u66f4\u65b0\u4e3a\u65b0\u503c\u3002\u5426\u5219\uff0c\u5904\u7406\u5668\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\u3002<\/li>\n<li>\u65e0\u8bba\u54ea\u79cd\u60c5\u51b5\uff0c\u5b83\u90fd\u4f1a\u5728CAS\u6307\u4ee4\u4e4b\u524d\u8fd4\u56de\u8be5\u4f4d\u7f6e\u7684\u503c\u3002<\/li>\n<li>CAS\u6709\u6548\u5730\u8bf4\u660e\u4e86\u201c\u6211\u8ba4\u4e3a\u4f4d\u7f6eV\u5e94\u8be5\u5305\u542b\u503cA\uff1b\u5982\u679c\u5305\u542b\u8be5\u503c\uff0c\u5219\u5c06B\u653e\u5230\u8fd9\u4e2a\u4f4d\u7f6e\uff1b\u5426\u5219\uff0c\u4e0d\u8981\u66f4\u6539\u8be5\u4f4d\u7f6e\uff0c\u53ea\u544a\u8bc9\u6211\u8fd9\u4e2a\u4f4d\u7f6e\u73b0\u5728\u7684\u503c\u5373\u53ef\u3002<\/li>\n<\/ul>\n<h5>1\u3001\u4e00\u4e2a CAS \u6d89\u53ca\u5230\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/h5>\n<p>\u5047\u8bbe\u5185\u5b58\u4e2d\u7684\u539f\u6570\u636eV\uff0c\u65e7\u7684\u9884\u671f\u503cA\uff0c\u9700\u8981\u4fee\u6539\u7684\u65b0\u503cB<\/p>\n<ul>\n<li><strong>\u6bd4\u8f83 A \u4e0e V \u662f\u5426\u76f8\u7b49<\/strong><\/li>\n<li><strong>\u5982\u679c\u6bd4\u8f83\u76f8\u7b49\uff0c\u5c06 B \u5199\u5165 V<\/strong><\/li>\n<li><strong>\u8fd4\u56de\u64cd\u4f5c\u662f\u5426\u6210\u529f<\/strong><\/li>\n<\/ul>\n<p><strong>2\u3001CAS\u7b97\u6cd5\u539f\u7406\u63cf\u8ff0<\/strong><\/p>\n<ul>\n<li>\u5728\u5bf9\u53d8\u91cf\u8fdb\u884c\u8ba1\u7b97\u4e4b\u524d(\u5982 ++ \u64cd\u4f5c)\uff0c\u9996\u5148\u8bfb\u53d6\u539f\u53d8\u91cf\u503c\uff0c\u79f0\u4e3a \u65e7\u7684\u9884\u671f\u503c A<\/li>\n<li>\u7136\u540e\u5728\u66f4\u65b0\u4e4b\u524d\u518d\u83b7\u53d6\u5f53\u524d\u5185\u5b58\u4e2d\u7684\u503c\uff0c\u79f0\u4e3a \u5f53\u524d\u5185\u5b58\u503c V<\/li>\n<li>\u5982\u679c A==V \u5219\u8bf4\u660e\u53d8\u91cf\u4ece\u672a\u88ab\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u8fc7\uff0c\u6b64\u65f6\u5c06\u4f1a\u5199\u5165\u65b0\u503c B<\/li>\n<li>\u5982\u679c A!=V \u5219\u8bf4\u660e\u53d8\u91cf\u5df2\u7ecf\u88ab\u5176\u4ed6\u7ebf\u7a0b\u4fee\u6539\u8fc7\uff0c\u5f53\u524d\u7ebf\u7a0b\u5e94\u5f53\u4ec0\u4e48\u4e5f\u4e0d\u505a\u3002<\/li>\n<\/ul>\n<pre>func compareAndSwap(old *int64 , expect *int64, new int64) bool {\r\n   if *old == *expect {\r\n      *old = new\r\n      return true\r\n   }\r\n   return false\r\n}<\/pre>\n<p>go\u7684sync\/atomic<\/p>\n<pre>\/\/ CompareAndSwapInt32 executes the compare-and-swap operation for an int32 value.\r\nfunc CompareAndSwapInt32(addr *int32, old, new int32) (swapped bool)\r\n\r\n\/\/ CompareAndSwapInt64 executes the compare-and-swap operation for an int64 value.\r\nfunc CompareAndSwapInt64(addr *int64, old, new int64) (swapped bool)\r\n\r\n\/\/ CompareAndSwapUint32 executes the compare-and-swap operation for a uint32 value.\r\nfunc CompareAndSwapUint32(addr *uint32, old, new uint32) (swapped bool)\r\n\r\n\/\/ CompareAndSwapUint64 executes the compare-and-swap operation for a uint64 value.\r\nfunc CompareAndSwapUint64(addr *uint64, old, new uint64) (swapped bool)<\/pre>\n<p>&nbsp;<\/p>\n<h5>3\u3001\u57fa\u4e8e\u94fe\u8868\u7684\u975e\u963b\u585e\u5806\u6808\u5b9e\u73b0<\/h5>\n<pre>template \r\nclass Stack { \r\n typedef struct Node { \r\n T data;\r\n Node* next;\r\n Node(const T&amp; d) : data(d), next(0) { } \r\n } Node;\r\n Node *top; \r\n public: \r\n Stack( ) : top(0) { }\r\n void push(const T&amp; data);\r\n T pop( ) throw (\u2026); \r\n};<\/pre>\n<p><strong>a\u3001\u5728\u975e\u963b\u585e\u5806\u6808\u4e2d\u538b\u5165\u6570\u636e(push)<\/strong><\/p>\n<pre>void Stack::push(const T&amp; data)\r\n{\r\n   Node *n = new Node(data);\r\n   while (1) {\r\n      n-&gt;next = top;\r\n      if (__sync_bool_compare_and_swap(&amp;top, n-&gt;next, n)) { \/\/ CAS\r\n         break;\r\n      }\r\n   }\r\n}<\/pre>\n<ul>\n<li>\u4ece\u5355\u4e00\u7ebf\u7a0b\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u8282\u70b9\uff0c\u5b83\u7684 next \u6307\u9488\u6307\u5411\u5806\u6808\u7684\u9876\u90e8\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u8c03\u7528 CAS \u5185\u7f6e\u51fd\u6570\uff0c\u628a\u65b0\u7684\u8282\u70b9\u590d\u5236\u5230 top \u4f4d\u7f6e\u3002<\/li>\n<li>\u4ece\u591a\u4e2a\u7ebf\u7a0b\u7684\u89d2\u5ea6\u6765\u770b\uff0c\u5b8c\u5168\u53ef\u80fd\u6709\u4e24\u4e2a\u6216\u66f4\u591a\u7ebf\u7a0b\u540c\u65f6\u8bd5\u56fe\u628a\u6570\u636e\u538b\u5165\u5806\u6808\u3002<\/li>\n<li>\u5047\u8bbe\u7ebf\u7a0b A \u8bd5\u56fe\u628a 20 \u538b\u5165\u5806\u6808\uff0c\u7ebf\u7a0b B \u8bd5\u56fe\u538b\u5165 30\uff0c\u800c\u7ebf\u7a0b A \u5148\u83b7\u5f97\u4e86\u65f6\u95f4\u7247\u3002<\/li>\n<li>\u4f46\u662f\uff0c\u5728 n-&gt;next = top \u6307\u4ee4\u7ed3\u675f\u4e4b\u540e\uff0c\u8c03\u5ea6\u7a0b\u5e8f\u6682\u505c\u4e86\u7ebf\u7a0b A\u3002<\/li>\n<li>\u73b0\u5728\uff0c\u7ebf\u7a0b B \u83b7\u5f97\u4e86\u65f6\u95f4\u7247\uff08\u5b83\u5f88\u5e78\u8fd0\uff09\uff0c\u5b83\u80fd\u591f\u5b8c\u6210 CAS\uff0c\u628a 30 \u538b\u5165\u5806\u6808\u540e\u7ed3\u675f\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u7ebf\u7a0b A \u6062\u590d\u6267\u884c\uff0c\u663e\u7136\u5bf9\u4e8e\u8fd9\u4e2a\u7ebf\u7a0b *top \u548c n-&gt;next \u4e0d\u5339\u914d\uff0c\u56e0\u4e3a\u7ebf\u7a0b B \u4fee\u6539\u4e86 top \u4f4d\u7f6e\u7684\u5185\u5bb9\u3002<\/li>\n<li>\u56e0\u6b64\uff0c\u4ee3\u7801\u56de\u5230\u5faa\u73af\u7684\u5f00\u5934\uff0c\u6307\u5411\u6b63\u786e\u7684 top \u6307\u9488\uff08\u7ebf\u7a0b B \u4fee\u6539\u540e\u7684\uff09\uff0c\u8c03\u7528 CAS\uff0c\u628a 20 \u538b\u5165\u5806\u6808\u540e\u7ed3\u675f\u3002<\/li>\n<li>\u6574\u4e2a\u8fc7\u7a0b\u6ca1\u6709\u4f7f\u7528\u4efb\u4f55\u9501\u3002<\/li>\n<\/ul>\n<h5>b\u3001\u4ece\u975e\u963b\u585e\u5806\u6808\u5f39\u51fa\u6570\u636e(pop)<\/h5>\n<pre>T Stack::pop( ) \r\n{ \r\n while (1) { \r\n   Node* result = top;\r\n   if (result == NULL) \r\n       throw std::string(\u201cCannot pop from empty stack\u201d); \r\n   if (top &amp;&amp; __sync_bool_compare_and_swap(&amp;top, result, result-&gt;next)) { \/\/ CAS\r\n      return result-&gt;data;\r\n    }\r\n  }\r\n}<\/pre>\n<p>\u8fd9\u6837\uff0c\u5373\u4f7f\u7ebf\u7a0b B \u5728\u7ebf\u7a0b A \u8bd5\u56fe\u5f39\u51fa\u6570\u636e\u7684\u540c\u65f6\u4fee\u6539\u4e86\u5806\u6808\u9876\uff0c\u4e5f\u53ef\u4ee5\u786e\u4fdd\u4e0d\u4f1a\u8df3\u8fc7\u5806\u6808\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<h5>4\u3001\u65e0\u9501\u961f\u5217\u7684\u94fe\u8868\u5b9e\u73b0<\/h5>\n<h5>a\u3001\u7528CAS\u5b9e\u73b0\u7684\u5165\u961f\u64cd\u4f5c<\/h5>\n<pre>EnQueue(x)\/\/\u8fdb\u961f\u5217\r\n{\r\n \/\/\u51c6\u5907\u65b0\u52a0\u5165\u7684\u7ed3\u70b9\u6570\u636e\r\n q = newrecord();\r\n q-&gt;value = x;\r\n q-&gt;next = NULL;\r\n \r\n do{\r\n p = tail; \/\/\u53d6\u94fe\u8868\u5c3e\u6307\u9488\u7684\u5feb\u7167\r\n }while( CAS(p-&gt;next, NULL, q) != TRUE); \/\/\u5982\u679c\u6ca1\u6709\u628a\u7ed3\u70b9\u94fe\u4e0a\uff0c\u518d\u8bd5\r\n \r\n CAS(tail, p, q); \/\/\u7f6e\u5c3e\u7ed3\u70b9\r\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u7a0b\u5e8f\u4e2d\u7684\u90a3\u4e2a do- while \u7684 Re-Try-Loo\u3002\u5c31\u662f\u8bf4\uff0c\u5f88\u6709\u53ef\u80fd\u6211\u5728\u51c6\u5907\u5728\u961f\u5217\u5c3e\u52a0\u5165\u7ed3\u70b9\u65f6\uff0c\u522b\u7684\u7ebf\u7a0b\u5df2\u7ecf\u52a0\u6210\u529f\u4e86\uff0c\u4e8e\u662ftail\u6307\u9488\u5c31\u53d8\u4e86\uff0c\u4e8e\u662f\u6211\u7684CAS\u8fd4\u56de\u4e86false\uff0c\u4e8e\u662f\u7a0b\u5e8f\u518d\u8bd5\uff0c\u76f4\u5230\u8bd5\u6210\u529f\u4e3a\u6b62\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u6211\u4eec\u7684\u201c\u7f6e\u5c3e\u7ed3\u70b9\u201d\u7684\u64cd\u4f5c\u4e0d\u5224\u65ad\u662f\u5426\u6210\u529f:<\/strong><\/p>\n<p>\u5982\u679c\u6709\u4e00\u4e2a\u7ebf\u7a0bT1\uff0c\u5b83\u7684while\u4e2d\u7684CAS\u5982\u679c\u6210\u529f\u7684\u8bdd\uff0c\u90a3\u4e48\u5176\u5b83\u6240\u6709\u968f\u540e\u7ebf\u7a0b\u7684CAS\u90fd\u4f1a\u5931\u8d25\uff0c\u7136\u540e\u5c31\u4f1a\u518d\u5faa\u73af\uff0c\u6b64\u65f6\uff0c\u5982\u679cT1 \u7ebf\u7a0b\u8fd8\u6ca1\u6709\u66f4\u65b0tail\u6307\u9488\uff0c\u5176\u5b83\u7684\u7ebf\u7a0b\u7ee7\u7eed\u5931\u8d25\uff0c\u56e0\u4e3atail-&gt;next\u4e0d\u662fNULL\u4e86\u3002\u76f4\u5230T1\u7ebf\u7a0b\u66f4\u65b0\u5b8ctail\u6307\u9488\uff0c\u4e8e\u662f\u5176\u5b83\u7684\u7ebf\u7a0b\u4e2d\u7684\u67d0\u4e2a\u7ebf\u7a0b\u5c31\u53ef\u4ee5\u5f97\u5230\u65b0\u7684tail\u6307\u9488\uff0c\u7ee7\u7eed\u5f80\u4e0b\u8d70\u4e86\u3002<\/p>\n<p><strong>\u8fd9\u91cc\u6709\u4e00\u4e2a\u6f5c\u5728\u7684\u95ee\u9898\u2014\u2014\u5982\u679cT1\u7ebf\u7a0b\u5728\u7528CAS\u66f4\u65b0tail\u6307\u9488\u7684\u4e4b\u524d\uff0c\u7ebf\u7a0b\u505c\u6389\u4e86\uff0c\u90a3\u4e48\u5176\u5b83\u7ebf\u7a0b\u5c31\u8fdb\u5165\u6b7b\u5faa\u73af\u4e86\u3002<\/strong><\/p>\n<p>\u4e0b\u9762\u662f\u6539\u826f\u7248\u7684EnQueue()<\/p>\n<pre>EnQueue(x)\/\/\u8fdb\u961f\u5217\u6539\u826f\u7248\r\n{\r\n q = newrecord();\r\n q-&gt;value = x;\r\n q-&gt;next = NULL;\r\n \r\n p = tail;\r\n oldp = p\r\n do{\r\n while(p-&gt;next != NULL)\r\n p = p-&gt;next;\r\n }while( CAS(p.next, NULL, q) != TRUE); \/\/\u5982\u679c\u6ca1\u6709\u628a\u7ed3\u70b9\u94fe\u4e0a\uff0c\u518d\u8bd5\r\n \r\n CAS(tail, oldp, q); \/\/\u7f6e\u5c3e\u7ed3\u70b9\r\n}<\/pre>\n<p>\u6211\u4eec\u8ba9\u6bcf\u4e2a\u7ebf\u7a0b\uff0c\u81ea\u5df1fetch \u6307\u9488 p \u5230\u94fe\u8868\u5c3e\u3002\u4f46\u662f\u8fd9\u6837\u7684fetch\u4f1a\u5f88\u5f71\u54cd\u6027\u80fd\u3002\u800c\u901a\u5b9e\u9645\u60c5\u51b5\u770b\u4e0b\u6765\uff0c99.9%\u7684\u60c5\u51b5\u4e0d\u4f1a\u6709\u7ebf\u7a0b\u505c\u8f6c\u7684\u60c5\u51b5\uff0c\u6240\u4ee5\uff0c\u66f4\u597d\u7684\u505a\u6cd5\u662f\uff0c\u4f60\u53ef\u4ee5\u63a5\u5408\u4e0a\u8ff0\u7684\u8fd9\u4e24\u4e2a\u7248\u672c\uff0c\u5982\u679cretry\u7684\u6b21\u6570\u8d85\u4e86\u4e00\u4e2a\u503c\u7684\u8bdd\uff08\u6bd4\u5982\u8bf43\u6b21\uff09\uff0c\u90a3\u4e48\uff0c\u5c31\u81ea\u5df1fetch\u6307\u9488\u3002<\/p>\n<h5>2\u3001\u7528CAS\u5b9e\u73b0\u7684\u51fa\u961f\u64cd\u4f5c<\/h5>\n<pre>DeQueue()\/\/\u51fa\u961f\u5217\r\n{\r\n do{\r\n p = head;\r\n if(p-&gt;next == NULL){\r\n returnERR_EMPTY_QUEUE;\r\n }\r\n while( CAS(head, p, p-&gt;next) != TRUE );\r\n returnp-&gt;next-&gt;value;\r\n}<\/pre>\n<p>DeQueue\u7684\u4ee3\u7801\u64cd\u4f5c\u7684\u662f head-&gt;next\uff0c\u800c\u4e0d\u662fhead\u672c\u8eab\u3002\u8fd9\u6837\u8003\u8651\u662f\u56e0\u4e3a\u4e00\u4e2a\u8fb9\u754c\u6761\u4ef6\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2adummy\u7684\u5934\u6307\u9488\u6765\u89e3\u51b3\u94fe\u8868\u4e2d\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0chead\u548ctail\u90fd\u6307\u5411\u540c\u4e00\u4e2a\u7ed3\u70b9\u7684\u95ee\u9898\uff0c\u8fd9\u6837EnQueue\u548cDeQueue\u8981\u4e92\u76f8\u6392\u65a5\u4e86\u3002<\/p>\n<p><strong>\u603b\u7ed3:\u4e0a\u8ff0\u6211\u4eec\u8bbe\u8ba1\u4e86\u652f\u6301\u5e76\u53d1\u8bbf\u95ee\u7684\u6570\u636e\u7ed3\u6784\u3002\u53ef\u4ee5\u770b\u5230\uff0c\u8bbe\u8ba1\u53ef\u4ee5\u57fa\u4e8e\u4e92\u65a5\u9501\uff0c\u4e5f\u53ef\u4ee5\u662f\u65e0\u9501\u7684\u3002\u65e0\u8bba\u91c7\u7528\u54ea\u79cd\u65b9\u5f0f\uff0c\u8981\u8003\u8651\u7684\u95ee\u9898\u4e0d\u4ec5\u4ec5\u662f\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u7684\u57fa\u672c\u529f\u80fd \u2014 \u5177\u4f53\u6765\u8bf4\uff0c\u5fc5\u987b\u4e00\u76f4\u8bb0\u4f4f\u7ebf\u7a0b\u4f1a\u4e89\u593a\u6267\u884c\u6743\uff0c\u8981\u8003\u8651\u7ebf\u7a0b\u91cd\u65b0\u6267\u884c\u65f6\u5982\u4f55\u6062\u590d\u64cd\u4f5c\u3002\u76ee\u524d\uff0c\u89e3\u51b3\u65b9\u6848\uff08\u5c24\u5176\u662f\u65e0\u9501\u89e3\u51b3\u65b9\u6848\uff09\u4e0e\u5e73\u53f0\/\u7f16\u8bd1\u5668\u7d27\u5bc6\u76f8\u5173\u3002<\/strong><\/p>\n<h3>\u4e09\u3001CAS\u7684ABA\u95ee\u9898<\/h3>\n<p><strong>ABA\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fdb\u7a0bP1\u5728\u5171\u4eab\u53d8\u91cf\u4e2d\u8bfb\u5230\u503c\u4e3aA<\/li>\n<li>P1\u88ab\u62a2\u5360\u4e86\uff0c\u8fdb\u7a0bP2\u6267\u884c<\/li>\n<li>P2\u628a\u5171\u4eab\u53d8\u91cf\u91cc\u7684\u503c\u4eceA\u6539\u6210\u4e86B\uff0c\u518d\u6539\u56de\u5230A\uff0c\u6b64\u65f6\u88abP1\u62a2\u5360\u3002<\/li>\n<li>P1\u56de\u6765\u770b\u5230\u5171\u4eab\u53d8\u91cf\u91cc\u7684\u503c\u6ca1\u6709\u88ab\u6539\u53d8\uff0c\u4e8e\u662f\u7ee7\u7eed\u6267\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u4e3e\u4f8b1\uff1a<\/strong><\/p>\n<p>\u6bd4\u5982\u4e0a\u8ff0\u7684DeQueue()\u51fd\u6570\uff0c\u56e0\u4e3a\u6211\u4eec\u8981\u8ba9head\u548ctail\u5206\u5f00\uff0c\u6240\u4ee5\u6211\u4eec\u5f15\u5165\u4e86\u4e00\u4e2adummy\u6307\u9488\u7ed9head\uff0c\u5f53\u6211\u4eec\u505aCAS\u7684\u4e4b\u524d\uff0c\u5982\u679chead\u7684\u90a3\u5757\u5185\u5b58\u88ab\u56de\u6536\u5e76\u88ab\u91cd\u7528\u4e86\uff0c\u800c\u91cd\u7528\u7684\u5185\u5b58\u53c8\u88abEnQueue()\u8fdb\u6765\u4e86\uff0c\u8fd9\u4f1a\u6709\u5f88\u5927\u7684\u95ee\u9898\u3002\uff08\u5185\u5b58\u7ba1\u7406\u4e2d\u91cd\u7528\u5185\u5b58\u57fa\u672c\u4e0a\u662f\u4e00\u79cd\u5f88\u5e38\u89c1\u7684\u884c\u4e3a\uff09<\/p>\n<p><strong>\u4e3e\u4f8b2\uff1a<\/strong><\/p>\n<ul>\n<li>\u6211\u4eec\u5047\u8bbe\u4e00\u4e2a\u63d0\u6b3e\u673a\u7684\u4f8b\u5b50\u3002\u5047\u8bbe\u6709\u4e00\u4e2a\u9075\u5faaCAS\u539f\u7406\u7684\u63d0\u6b3e\u673a\uff0c\u5c0f\u7070\u6709100\u5143\u5b58\u6b3e\uff0c\u8981\u7528\u8fd9\u4e2a\u63d0\u6b3e\u673a\u6765\u63d0\u6b3e50\u5143\u3002<\/li>\n<li>\u7531\u4e8e\u63d0\u6b3e\u673a\u786c\u4ef6\u51fa\u4e86\u70b9\u95ee\u9898\uff0c\u5c0f\u7070\u7684\u63d0\u6b3e\u64cd\u4f5c\u88ab\u540c\u65f6\u63d0\u4ea4\u4e86\u4e24\u6b21\uff0c\u5f00\u542f\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u662f\u83b7\u53d6\u5f53\u524d\u503c100\u5143\uff0c\u8981\u66f4\u65b0\u621050\u5143\u3002<\/li>\n<li>\u7406\u60f3\u60c5\u51b5\u4e0b\uff0c\u5e94\u8be5\u4e00\u4e2a\u7ebf\u7a0b\u66f4\u65b0\u6210\u529f\uff0c\u4e00\u4e2a\u7ebf\u7a0b\u66f4\u65b0\u5931\u8d25\uff0c\u5c0f\u7070\u7684\u5b58\u6b3e\u503c\u88ab\u6263\u4e00\u6b21\u3002<\/li>\n<li>\u7ebf\u7a0b1\u9996\u5148\u6267\u884c\u6210\u529f\uff0c\u628a\u4f59\u989d\u4ece100\u6539\u621050.\u7ebf\u7a0b2\u56e0\u4e3a\u67d0\u79cd\u539f\u56e0\u963b\u585e\u3002\u8fd9\u65f6\uff0c\u5c0f\u7070\u7684\u5988\u5988\u521a\u597d\u7ed9\u5c0f\u7070\u6c47\u6b3e50\u5143\u3002<\/li>\n<li>\u7ebf\u7a0b2\u4ecd\u7136\u662f\u963b\u585e\u72b6\u6001\uff0c\u7ebf\u7a0b3\u6267\u884c\u6210\u529f\uff0c\u628a\u4f59\u989d\u4ece50\u6539\u6210\u4e86100\u3002<\/li>\n<li>\u7ebf\u7a0b2\u6062\u590d\u8fd0\u884c\uff0c\u7531\u4e8e\u963b\u585e\u4e4b\u524d\u83b7\u5f97\u4e86\u201c\u5f53\u524d\u503c\u201d100\uff0c\u5e76\u4e14\u7ecf\u8fc7compare\u68c0\u6d4b\uff0c\u6b64\u65f6\u5b58\u6b3e\u5b9e\u9645\u503c\u4e5f\u662f100\uff0c\u6240\u4ee5\u4f1a\u6210\u529f\u628a\u53d8\u91cf\u503c100\u66f4\u65b0\u621050\u3002<\/li>\n<li>\u539f\u672c\u7ebf\u7a0b2\u5e94\u5f53\u63d0\u4ea4\u5931\u8d25\uff0c\u5c0f\u7070\u7684\u6b63\u786e\u4f59\u989d\u5e94\u8be5\u4fdd\u6301100\u5143\uff0c\u7ed3\u679c\u7531\u4e8eABA\u95ee\u9898\u63d0\u4ea4\u6210\u529f\u4e86\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3ABA\u95ee\u9898<\/strong><\/p>\n<p>\u771f\u6b63\u8981\u505a\u5230\u4e25\u8c28\u7684CAS\u673a\u5236\uff0c\u6211\u4eec\u5728compare\u9636\u6bb5\u4e0d\u4ec5\u8981\u6bd4\u8f83\u671f\u671b\u503cA\u548c\u5730\u5740V\u4e2d\u7684\u5b9e\u9645\u503c\uff0c\u8fd8\u8981\u6bd4\u8f83\u53d8\u91cf\u7684\u7248\u672c\u53f7\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<p><strong>\u4e3e\u4e2a\u6817\u5b50\uff1a<\/strong><\/p>\n<ul>\n<li>\u5047\u8bbe\u5730\u5740V\u4e2d\u5b58\u50a8\u7740\u53d8\u91cf\u503cA\uff0c\u5f53\u524d\u7248\u672c\u53f7\u662f01\u3002\u7ebf\u7a0b1\u83b7\u53d6\u4e86\u5f53\u524d\u503cA\u548c\u7248\u672c\u53f701\uff0c\u60f3\u8981\u66f4\u65b0\u4e3aB\uff0c\u4f46\u662f\u88ab\u963b\u585e\u4e86\u3002<\/li>\n<li>\u8fd9\u65f6\u5019\uff0c\u5185\u5b58\u5730\u5740V\u4e2d\u53d8\u91cf\u53d1\u751f\u4e86\u591a\u6b21\u6539\u53d8\uff0c\u7248\u672c\u53f7\u63d0\u5347\u4e3a03\uff0c\u4f46\u662f\u53d8\u91cf\u503c\u4ecd\u7136\u662fA\u3002<\/li>\n<li>\u968f\u540e\u7ebf\u7a0b1\u6062\u590d\u8fd0\u884c\uff0c\u8fdb\u884ccompare\u64cd\u4f5c\u3002\u7ecf\u8fc7\u6bd4\u8f83\uff0c\u7ebf\u7a0b1\u6240\u83b7\u5f97\u7684\u503c\u548c\u5730\u5740\u7684\u5b9e\u9645\u503c\u90fd\u662fA\uff0c\u4f46\u662f\u7248\u672c\u53f7\u4e0d\u76f8\u7b49\uff0c\u6240\u4ee5\u8fd9\u4e00\u6b21\u66f4\u65b0\u5931\u8d25\u3002<\/li>\n<\/ul>\n<h3>\u56db\u3001CAS\u7684\u95ee\u9898<\/h3>\n<h5>1\u3001ABA\u95ee\u9898<\/h5>\n<p>\u56e0\u4e3aCAS\u9700\u8981\u5728\u64cd\u4f5c\u503c\u7684\u65f6\u5019\uff0c\u68c0\u67e5\u503c\u6709\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u6ca1\u6709\u53d1\u751f\u53d8\u5316\u624d\u53bb\u66f4\u65b0\u3002<br \/>\n\u4f46\u662f\u5982\u679c\u4e00\u4e2a\u503c\u539f\u6765\u662fA\u53d8\u6210\u4e86B\uff0c\u53c8\u53d8\u6210\u4e86A\uff0cCAS\u68c0\u67e5\u4f1a\u5224\u65ad\u8be5\u503c\u672a\u53d1\u751f\u53d8\u5316\uff0c\u5b9e\u9645\u5374\u53d8\u5316\u4e86\u3002<\/p>\n<p>\u89e3\u51b3\u601d\u8def\uff1a\u589e\u52a0\u7248\u672c\u53f7\uff0c\u6bcf\u6b21\u53d8\u91cf\u66f4\u65b0\u65f6\u628a\u7248\u672c\u53f7+1\uff0cA-B-A\u5c31\u53d8\u6210\u4e861A-2B-3A\u3002JDK5\u4e4b\u540e\u7684atomic\u5305\u63d0\u4f9b\u4e86AtomicStampedReference\u6765\u89e3\u51b3ABA\u95ee\u9898\uff0c\u5b83\u7684compareAndSet\u65b9\u6cd5\u4f1a\u9996\u5148\u68c0\u67e5\u5f53\u524d\u5f15\u7528\u662f\u5426\u7b49\u4e8e\u9884\u671f\u5f15\u7528\uff0c\u5e76\u4e14\u5f53\u524d\u6807\u5fd7\u662f\u5426\u7b49\u4e8e\u9884\u671f\u6807\u5fd7\u3002\u5168\u90e8\u76f8\u7b49\uff0c\u624d\u4f1a\u4ee5\u539f\u5b50\u65b9\u5f0f\u5c06\u8be5\u5f15\u7528\u3001\u8be5\u6807\u5fd7\u7684\u503c\u8bbe\u7f6e\u4e3a\u66f4\u65b0\u503c\u3002<\/p>\n<h5>2. \u65f6\u95f4\u957f\u3001\u5f00\u9500\u5927<\/h5>\n<p>\u81ea\u65cbCAS\u5982\u679c\u957f\u65f6\u95f4\u4e0d\u6210\u529f\uff0c\u4f1a\u7ed9CPU\u5e26\u6765\u975e\u5e38\u5927\u7684\u6267\u884c\u5f00\u9500\u3002<\/p>\n<h5>3. \u53ea\u80fd\u4fdd\u8bc1\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\u7684\u539f\u5b50\u64cd\u4f5c<\/h5>\n<p>\u5bf9\u4e00\u4e2a\u5171\u4eab\u53d8\u91cf\u6267\u884c\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u5faa\u73afCAS\u65b9\u5f0f\u786e\u4fdd\u539f\u5b50\u64cd\u4f5c\u3002\u4f46\u662f\u5bf9\u591a\u4e2a\u5171\u4eab\u53d8\u91cf\uff0c\u5c31\u4e0d\u7075\u4e86\u3002<\/p>\n<p>\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528\u9501\uff0c\u6216\u628a\u591a\u4e2a\u5171\u4eab\u53d8\u91cf\u5408\u5e76\u4e3a1\u4e2a\u5171\u4eab\u53d8\u91cf\uff0c\u5982i=2,j=a,\u5408\u5e76\u4e3aij=2a\u3002\u7136\u540e\u7528CAS\u64cd\u4f5cij\u3002\u5728JDK5\u540e\uff0c\u63d0\u4f9b\u4e86AtomicReference\u7c7b\u6765\u4fdd\u8bc1\u5bf9\u8c61\u95f4\u7684\u539f\u5b50\u6027\uff0c\u53ef\u4ee5\u628a\u591a\u4e2a\u5171\u4eab\u53d8\u91cf\u653e\u5728\u4e00\u4e2a\u5bf9\u8c61\u91cc\u8fdb\u884cCAS\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9501\u673a\u5236 \u5e38\u7528\u7684\u9501\u673a\u5236\u6709\u4e24\u79cd\uff1a\u60b2\u89c2\u9501\u3001\u4e50\u89c2\u9501\uff0c\u4e0b\u9762\u6211\u4eec\u5206\u522b\u4ecb\u7ecd\u4e0b\u3002 1\u3001\u60b2\u89c2\u9501 \u5047\u5b9a\u4f1a\u53d1\u751f\u5e76\u53d1\u51b2\u7a81\uff0c\u5c4f\u853d\u4e00\u5207\u53ef\u80fd\u8fdd\u53cd\u6570\u636e\u5b8c\u6574\u6027\u7684\u64cd\u4f5c\u3002 \u60b2\u89c2\u9501\u7684\u5b9e\u73b0\uff0c\u5f80\u5f80\u4f9d\u9760\u5e95\u5c42\u63d0\u4f9b\u7684\u9501\u673a\u5236\u3002 \u60b2\u89c2\u9501\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\uff0c\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u3002 &nbsp; 2\u3001\u4e50\u89c2\u9501 \u5047\u8bbe\u4e0d\u4f1a\u53d1\u751f\u5e76\u53d1\u51b2\u7a81\uff0c\u6bcf\u6b21\u4e0d\u52a0\u9501\u800c\u662f\u5047\u8bbe\u6ca1\u6709\u51b2\u7a81\u800c\u53bb\u5b8c\u6210\u67d0\u9879\u64cd\u4f5c\uff0c\u53ea\u5728\u63d0\u4ea4\u64cd\u4f5c\u65f6\u68c0\u67e5\u662f\u5426\u8fdd\u53cd\u6570\u636e\u5b8c\u6574\u6027\u3002 \u5982\u679c\u56e0\u4e3a\u51b2\u7a81\u5931\u8d25\u5c31\u91cd\u8bd5\uff0c\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002 \u4e50\u89c2\u9501\u5927\u591a\u662f\u57fa\u4e8e\u6570\u636e\u7248\u672c\u8bb0\u5f55\u673a\u5236\u5b9e\u73b0\u3002 \u4e3a\u6570\u636e\u589e\u52a0\u4e00\u4e2a\u7248\u672c\u6807\u8bc6\uff0c\u6bd4\u5982\u5728\u57fa\u4e8e\u6570\u636e\u5e93\u8868\u7684\u7248\u672c\u89e3\u51b3\u65b9\u6848\u4e2d\uff0c\u4e00\u822c\u662f\u901a\u8fc7\u4e3a\u6570\u636e\u5e93\u8868\u589e\u52a0\u4e00\u4e2a \u201cversion\u201d \u5b57\u6bb5\u6765\u5b9e\u73b0\u3002\u8bfb\u53d6\u51fa\u6570\u636e\u65f6\uff0c\u5c06\u6b64\u7248\u672c\u53f7\u4e00\u540c\u8bfb\u51fa\uff0c\u4e4b\u540e\u66f4\u65b0\u65f6\uff0c\u5bf9\u6b64\u7248\u672c\u53f7\u52a0\u4e00\u3002 \u6b64\u65f6\uff0c\u5c06\u63d0\u4ea4\u6570\u636e\u7684\u7248\u672c\u6570\u636e\u4e0e\u6570\u636e\u5e93\u8868\u5bf9\u5e94\u8bb0\u5f55\u7684\u5f53\u524d\u7248\u672c\u4fe1\u606f\u8fdb\u884c\u6bd4\u5bf9\uff0c\u5982\u679c\u63d0\u4ea4\u7684\u6570\u636e\u7248\u672c\u53f7\u5927\u4e8e\u6570\u636e\u5e93\u8868\u5f53\u524d\u7248\u672c\u53f7\uff0c\u5219\u4e88\u4ee5\u66f4\u65b0\uff0c\u5426\u5219\u8ba4\u4e3a\u662f\u8fc7\u671f\u6570\u636e\u3002 \u4e50\u89c2\u9501\u7684\u7f3a\u70b9\u662f\u4e0d\u80fd\u89e3\u51b3\u810f\u8bfb\u7684\u95ee\u9898\u3002 \u5728\u5b9e\u9645\u751f\u4ea7\u73af\u5883\u91cc\u8fb9,\u5982\u679c\u5e76\u53d1\u91cf\u4e0d\u5927\u4e14\u4e0d\u5141\u8bb8\u810f\u8bfb\uff0c\u53ef\u4ee5\u4f7f\u7528\u60b2\u89c2\u9501\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002 \u5982\u679c\u7cfb\u7edf\u7684\u5e76\u53d1\u975e\u5e38\u5927\u7684\u8bdd,\u60b2\u89c2\u9501\u5b9a\u4f1a\u5e26\u6765\u975e\u5e38\u5927\u7684\u6027\u80fd\u95ee\u9898,\u6240\u4ee5\u6211\u4eec\u5c31\u8981\u9009\u62e9\u4e50\u89c2\u9501\u5b9a\u7684\u65b9\u6cd5\u3002 3\u3001\u9501\u673a\u5236\u5b58\u5728\u7684\u95ee\u9898 \u5728\u591a\u7ebf\u7a0b\u7ade\u4e89\u4e0b\uff0c\u52a0\u9501\u3001\u91ca\u653e\u9501\u4f1a\u5bfc\u81f4\u6bd4\u8f83\u591a\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u8c03\u5ea6\u5ef6\u65f6\uff0c\u5f15\u8d77\u6027\u80fd\u95ee\u9898\u3002 \u4e00\u4e2a\u7ebf\u7a0b\u6301\u6709\u9501\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u6b64\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\u3002 \u5982\u679c\u4e00\u4e2a\u4f18\u5148\u7ea7\u9ad8\u7684\u7ebf\u7a0b\u7b49\u5f85\u4e00\u4e2a\u4f18\u5148\u7ea7\u4f4e\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u4f1a\u5bfc\u81f4\u4f18\u5148\u7ea7\u5012\u7f6e\uff0c\u5f15\u8d77\u6027\u80fd\u98ce\u9669\u3002 volatile\u662f\u4e0d\u9519\u7684\u673a\u5236\uff0c\u4f46\u662fvolatile\u4e0d\u80fd\u4fdd\u8bc1\u539f\u5b50\u6027\u3002\u56e0\u6b64\u5bf9\u4e8e\u540c\u6b65\u6700\u7ec8\u8fd8\u662f\u8981\u56de\u5230\u9501\u673a\u5236\u4e0a\u6765\u3002 4\u3001\u4e24\u79cd\u9501\u603b\u7ed3 \u72ec\u5360\u9501\u662f\u4e00\u79cd\u60b2\u89c2\u9501\uff0csynchronized\u5c31\u662f\u4e00\u79cd\u72ec\u5360\u9501\uff0c\u4f1a\u5bfc\u81f4\u5176\u5b83\u6240\u6709\u9700\u8981\u9501\u7684\u7ebf\u7a0b\u6302\u8d77\uff0c\u7b49\u5f85\u6301\u6709\u9501\u7684\u7ebf\u7a0b\u91ca\u653e\u9501\u3002 \u6240\u8c13\u4e50\u89c2\u9501\u5c31\u662f\uff0c\u6bcf\u6b21\u4e0d\u52a0\u9501\u800c\u662f\u5047\u8bbe\u6ca1\u6709\u51b2\u7a81\u800c\u53bb\u5b8c\u6210\u67d0\u9879\u64cd\u4f5c\uff0c\u5982\u679c\u56e0\u4e3a\u51b2\u7a81\u5931\u8d25\u5c31\u91cd\u8bd5\uff0c\u76f4\u5230\u6210\u529f\u4e3a\u6b62\u3002 \u4e8c\u3001CAS\uff08Compare &amp; Set\/Compare AND Swap\uff09 CAS\u662f\u89e3\u51b3\u591a\u7ebf\u7a0b\u5e76\u884c\u60c5\u51b5\u4e0b\u4f7f\u7528\u9501\u9020\u6210\u6027\u80fd\u635f\u8017\u7684\u4e00\u79cd\u673a\u5236\u3002 CAS\u64cd\u4f5c\u5305\u542b\u4e09\u4e2a\u64cd\u4f5c\u6570\u2014\u2014\u5185\u5b58\u4f4d\u7f6e\uff08V\uff09\u3001\u9884\u671f\u539f\u503c\uff08A\uff09\u3001\u65b0\u503c(B)\u3002 \u5982\u679c\u5185\u5b58\u4f4d\u7f6e\u7684\u503c\u4e0e\u9884\u671f\u539f\u503c\u76f8\u5339\u914d\uff0c\u90a3\u4e48\u5904\u7406\u5668\u4f1a\u81ea\u52a8\u5c06\u8be5\u4f4d\u7f6e\u503c\u66f4\u65b0\u4e3a\u65b0\u503c\u3002\u5426\u5219\uff0c\u5904\u7406\u5668\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\u3002 \u65e0\u8bba\u54ea\u79cd\u60c5\u51b5\uff0c\u5b83\u90fd\u4f1a\u5728CAS\u6307\u4ee4\u4e4b\u524d\u8fd4\u56de\u8be5\u4f4d\u7f6e\u7684\u503c\u3002 CAS\u6709\u6548\u5730\u8bf4\u660e\u4e86\u201c\u6211\u8ba4\u4e3a\u4f4d\u7f6eV\u5e94\u8be5\u5305\u542b\u503cA\uff1b\u5982\u679c\u5305\u542b\u8be5\u503c\uff0c\u5219\u5c06B\u653e\u5230\u8fd9\u4e2a\u4f4d\u7f6e\uff1b\u5426\u5219\uff0c\u4e0d\u8981\u66f4\u6539\u8be5\u4f4d\u7f6e\uff0c\u53ea\u544a\u8bc9\u6211\u8fd9\u4e2a\u4f4d\u7f6e\u73b0\u5728\u7684\u503c\u5373\u53ef\u3002 1\u3001\u4e00\u4e2a CAS \u6d89\u53ca\u5230\u4ee5\u4e0b\u64cd\u4f5c\uff1a \u5047\u8bbe\u5185\u5b58\u4e2d\u7684\u539f\u6570\u636eV\uff0c\u65e7\u7684\u9884\u671f\u503cA\uff0c\u9700\u8981\u4fee\u6539\u7684\u65b0\u503cB \u6bd4\u8f83 A \u4e0e V \u662f\u5426\u76f8\u7b49 \u5982\u679c\u6bd4\u8f83\u76f8\u7b49\uff0c\u5c06 B \u5199\u5165 V \u8fd4\u56de\u64cd\u4f5c\u662f\u5426\u6210\u529f 2\u3001CAS\u7b97\u6cd5\u539f\u7406\u63cf\u8ff0 \u5728\u5bf9\u53d8\u91cf\u8fdb\u884c\u8ba1\u7b97\u4e4b\u524d(\u5982 ++ \u64cd\u4f5c)\uff0c\u9996\u5148\u8bfb\u53d6\u539f\u53d8\u91cf\u503c\uff0c\u79f0\u4e3a \u65e7\u7684\u9884\u671f\u503c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15305","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15305"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15305\/revisions"}],"predecessor-version":[{"id":15306,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15305\/revisions\/15306"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}