{"id":577,"date":"2013-11-15T10:40:34","date_gmt":"2013-11-15T02:40:34","guid":{"rendered":"http:\/\/coderbee.net\/?p=577"},"modified":"2019-01-06T21:24:10","modified_gmt":"2019-01-06T13:24:10","slug":"%e8%87%aa%e6%97%8b%e9%94%81%e3%80%81%e6%8e%92%e9%98%9f%e8%87%aa%e6%97%8b%e9%94%81%e3%80%81mcs%e9%94%81%e3%80%81clh%e9%94%81","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/concurrent\/20131115\/577","title":{"rendered":"\u81ea\u65cb\u9501\u3001\u6392\u961f\u81ea\u65cb\u9501\u3001MCS\u9501\u3001CLH\u9501"},"content":{"rendered":"<h3>\u81ea\u65cb\u9501\uff08Spin lock\uff09<\/h3>\n<p>\u81ea\u65cb\u9501\u662f\u6307\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u83b7\u53d6\u67d0\u4e2a\u9501\u65f6\uff0c\u5982\u679c\u8be5\u9501\u5df2\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5360\u7528\uff0c\u5c31\u4e00\u76f4\u5faa\u73af\u68c0\u6d4b\u9501\u662f\u5426\u88ab\u91ca\u653e\uff0c\u800c\u4e0d\u662f\u8fdb\u5165\u7ebf\u7a0b\u6302\u8d77\u6216\u7761\u7720\u72b6\u6001\u3002<\/p>\n<p>\u81ea\u65cb\u9501\u9002\u7528\u4e8e\u9501\u4fdd\u62a4\u7684\u4e34\u754c\u533a\u5f88\u5c0f\u7684\u60c5\u51b5\uff0c\u4e34\u754c\u533a\u5f88\u5c0f\u7684\u8bdd\uff0c\u9501\u5360\u7528\u7684\u65f6\u95f4\u5c31\u5f88\u77ed\u3002<\/p>\n<h4>\u7b80\u5355\u7684\u5b9e\u73b0<\/h4>\n<pre><code class=\"java\">import java.util.concurrent.atomic.AtomicReference;\r\n\r\npublic class SpinLock {\r\n   private AtomicReference&lt;Thread&gt; owner = new AtomicReference&lt;Thread&gt;();\r\n\r\n   public void lock() {\r\n       Thread currentThread = Thread.currentThread();\r\n\r\n              \/\/ \u5982\u679c\u9501\u672a\u88ab\u5360\u7528\uff0c\u5219\u8bbe\u7f6e\u5f53\u524d\u7ebf\u7a0b\u4e3a\u9501\u7684\u62e5\u6709\u8005\r\n       while (!owner.compareAndSet(null, currentThread)) {\r\n       }\r\n   }\r\n\r\n   public void unlock() {\r\n       Thread currentThread = Thread.currentThread();\r\n\r\n              \/\/ \u53ea\u6709\u9501\u7684\u62e5\u6709\u8005\u624d\u80fd\u91ca\u653e\u9501\r\n       owner.compareAndSet(currentThread, null);\r\n   }\r\n}\r\n<\/code><\/pre>\n<p>SimpleSpinLock\u91cc\u6709\u4e00\u4e2aowner\u5c5e\u6027\u6301\u6709\u9501\u5f53\u524d\u62e5\u6709\u8005\u7684\u7ebf\u7a0b\u7684\u5f15\u7528\uff0c\u5982\u679c\u8be5\u5f15\u7528\u4e3anull\uff0c\u5219\u8868\u793a\u9501\u672a\u88ab\u5360\u7528\uff0c\u4e0d\u4e3anull\u5219\u88ab\u5360\u7528\u3002<\/p>\n<p>\u8fd9\u91cc\u7528AtomicReference\u662f\u4e3a\u4e86\u4f7f\u7528\u5b83\u7684\u539f\u5b50\u6027\u7684compareAndSet\u65b9\u6cd5\uff08CAS\u64cd\u4f5c\uff09\uff0c\u89e3\u51b3\u4e86\u591a\u7ebf\u7a0b\u5e76\u53d1\u64cd\u4f5c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u95ee\u9898\uff0c\u786e\u4fdd\u5176\u4ed6\u7ebf\u7a0b\u53ef\u4ee5\u770b\u5230\u9501\u7684\u771f\u5b9e\u72b6\u6001\u3002<br \/>\n<!--more--><\/p>\n<h4>\u7f3a\u70b9<\/h4>\n<ol>\n<li>CAS\u64cd\u4f5c\u9700\u8981\u786c\u4ef6\u7684\u914d\u5408\uff1b<\/li>\n<li>\u4fdd\u8bc1\u5404\u4e2aCPU\u7684\u7f13\u5b58\uff08L1\u3001L2\u3001L3\u3001\u8de8CPU Socket\u3001\u4e3b\u5b58\uff09\u7684\u6570\u636e\u4e00\u81f4\u6027\uff0c\u901a\u8baf\u5f00\u9500\u5f88\u5927\uff0c\u5728\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e0a\u66f4\u4e25\u91cd\uff1b<\/li>\n<li>\u6ca1\u6cd5\u4fdd\u8bc1\u516c\u5e73\u6027\uff0c\u4e0d\u4fdd\u8bc1\u7b49\u5f85\u8fdb\u7a0b\/\u7ebf\u7a0b\u6309\u7167FIFO\u987a\u5e8f\u83b7\u5f97\u9501\u3002<\/li>\n<\/ol>\n<h3>Ticket Lock<\/h3>\n<p>Ticket Lock \u662f\u4e3a\u4e86\u89e3\u51b3\u4e0a\u9762\u7684\u516c\u5e73\u6027\u95ee\u9898\uff0c\u7c7b\u4f3c\u4e8e\u73b0\u5b9e\u4e2d\u94f6\u884c\u67dc\u53f0\u7684\u6392\u961f\u53eb\u53f7\uff1a\u9501\u62e5\u6709\u4e00\u4e2a\u670d\u52a1\u53f7\uff0c\u8868\u793a\u6b63\u5728\u670d\u52a1\u7684\u7ebf\u7a0b\uff0c\u8fd8\u6709\u4e00\u4e2a\u6392\u961f\u53f7\uff1b\u6bcf\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u83b7\u53d6\u9501\u4e4b\u524d\u5148\u62ff\u4e00\u4e2a\u6392\u961f\u53f7\uff0c\u7136\u540e\u4e0d\u65ad\u8f6e\u8be2\u9501\u7684\u5f53\u524d\u670d\u52a1\u53f7\u662f\u5426\u662f\u81ea\u5df1\u7684\u6392\u961f\u53f7\uff0c\u5982\u679c\u662f\uff0c\u5219\u8868\u793a\u81ea\u5df1\u62e5\u6709\u4e86\u9501\uff0c\u4e0d\u662f\u5219\u7ee7\u7eed\u8f6e\u8be2\u3002<\/p>\n<p>\u5f53\u7ebf\u7a0b\u91ca\u653e\u9501\u65f6\uff0c\u5c06\u670d\u52a1\u53f7\u52a01\uff0c\u8fd9\u6837\u4e0b\u4e00\u4e2a\u7ebf\u7a0b\u770b\u5230\u8fd9\u4e2a\u53d8\u5316\uff0c\u5c31\u9000\u51fa\u81ea\u65cb\u3002<\/p>\n<h4>\u7b80\u5355\u7684\u5b9e\u73b0<\/h4>\n<pre><code class=\"java\">import java.util.concurrent.atomic.AtomicInteger;\r\n\r\npublic class TicketLock {\r\n   private AtomicInteger serviceNum = new AtomicInteger(); \/\/ \u670d\u52a1\u53f7\r\n   private AtomicInteger ticketNum = new AtomicInteger(); \/\/ \u6392\u961f\u53f7\r\n\r\n   public int lock() {\r\n         \/\/ \u9996\u5148\u539f\u5b50\u6027\u5730\u83b7\u5f97\u4e00\u4e2a\u6392\u961f\u53f7\r\n         int myTicketNum = ticketNum.getAndIncrement();\r\n\r\n              \/\/ \u53ea\u8981\u5f53\u524d\u670d\u52a1\u53f7\u4e0d\u662f\u81ea\u5df1\u7684\u5c31\u4e0d\u65ad\u8f6e\u8be2\r\n       while (serviceNum.get() != myTicketNum) {\r\n       }\r\n\r\n       return myTicketNum;\r\n    }\r\n\r\n    public void unlock(int myTicket) {\r\n        \/\/ \u53ea\u6709\u5f53\u524d\u7ebf\u7a0b\u62e5\u6709\u8005\u624d\u80fd\u91ca\u653e\u9501\r\n        int next = myTicket + 1;\r\n        serviceNum.compareAndSet(myTicket, next);\r\n    }\r\n}\r\n<\/code><\/pre>\n<h4>\u7f3a\u70b9<\/h4>\n<p>Ticket Lock \u867d\u7136\u89e3\u51b3\u4e86\u516c\u5e73\u6027\u7684\u95ee\u9898\uff0c\u4f46\u662f\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e0a\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\/\u7ebf\u7a0b\u5360\u7528\u7684\u5904\u7406\u5668\u90fd\u5728\u8bfb\u5199\u540c\u4e00\u4e2a\u53d8\u91cfserviceNum \uff0c\u6bcf\u6b21\u8bfb\u5199\u64cd\u4f5c\u90fd\u5fc5\u987b\u5728\u591a\u4e2a\u5904\u7406\u5668\u7f13\u5b58\u4e4b\u95f4\u8fdb\u884c\u7f13\u5b58\u540c\u6b65\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u7e41\u91cd\u7684\u7cfb\u7edf\u603b\u7ebf\u548c\u5185\u5b58\u7684\u6d41\u91cf\uff0c\u5927\u5927\u964d\u4f4e\u7cfb\u7edf\u6574\u4f53\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4e0b\u9762\u4ecb\u7ecd\u7684CLH\u9501\u548cMCS\u9501\u90fd\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u3002<\/p>\n<p>MCS \u6765\u81ea\u4e8e\u5176\u53d1\u660e\u4eba\u540d\u5b57\u7684\u9996\u5b57\u6bcd\uff1a John Mellor-Crummey\u548cMichael Scott\u3002<\/p>\n<p>CLH\u7684\u53d1\u660e\u4eba\u662f\uff1aCraig\uff0cLandin and Hagersten\u3002<\/p>\n<h3>MCS\u9501<\/h3>\n<p>MCS Spinlock \u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u53ef\u6269\u5c55\u3001\u9ad8\u6027\u80fd\u3001\u516c\u5e73\u7684\u81ea\u65cb\u9501\uff0c\u7533\u8bf7\u7ebf\u7a0b\u53ea\u5728\u672c\u5730\u53d8\u91cf\u4e0a\u81ea\u65cb\uff0c\u76f4\u63a5\u524d\u9a71\u8d1f\u8d23\u901a\u77e5\u5176\u7ed3\u675f\u81ea\u65cb\uff0c\u4ece\u800c\u6781\u5927\u5730\u51cf\u5c11\u4e86\u4e0d\u5fc5\u8981\u7684\u5904\u7406\u5668\u7f13\u5b58\u540c\u6b65\u7684\u6b21\u6570\uff0c\u964d\u4f4e\u4e86\u603b\u7ebf\u548c\u5185\u5b58\u7684\u5f00\u9500\u3002<\/p>\n<pre><code class=\"java\">import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;\r\n\r\npublic class MCSLock {\r\n    public static class MCSNode {\r\n        volatile MCSNode next;\r\n        volatile boolean isBlock = true; \/\/ \u9ed8\u8ba4\u662f\u5728\u7b49\u5f85\u9501\r\n    }\r\n\r\n    volatile MCSNode queue;\/\/ \u6307\u5411\u6700\u540e\u4e00\u4e2a\u7533\u8bf7\u9501\u7684MCSNode\r\n    private static final AtomicReferenceFieldUpdater<MCSLock, MCSNode> UPDATER = AtomicReferenceFieldUpdater\r\n            .newUpdater(MCSLock.class, MCSNode.class, \"queue\");\r\n\r\n    public void lock(MCSNode currentThread) {\r\n        MCSNode predecessor = UPDATER.getAndSet(this, currentThread);\/\/ step 1\r\n        if (predecessor != null) {\r\n            predecessor.next = currentThread;\/\/ step 2\r\n\r\n            while (currentThread.isBlock) {\/\/ step 3\r\n            }\r\n        }else { \/\/ \u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u4f7f\u7528\u9501\uff0c\u6ca1\u6709\u524d\u9a71\u6765\u901a\u77e5\u5b83\uff0c\u6240\u4ee5\u5f97\u81ea\u5df1\u6807\u8bb0\u81ea\u5df1\u4e3a\u975e\u963b\u585e\r\n               currentThread. isBlock = false;\r\n          }\r\n    }\r\n\r\n    public void unlock(MCSNode currentThread) {\r\n        if (currentThread.isBlock) {\/\/ \u9501\u62e5\u6709\u8005\u8fdb\u884c\u91ca\u653e\u9501\u624d\u6709\u610f\u4e49\r\n            return;\r\n        }\r\n\r\n        if (currentThread.next == null) {\/\/ \u68c0\u67e5\u662f\u5426\u6709\u4eba\u6392\u5728\u81ea\u5df1\u540e\u9762\r\n            if (UPDATER.compareAndSet(this, currentThread, null)) {\/\/ step 4\r\n                \/\/ compareAndSet\u8fd4\u56detrue\u8868\u793a\u786e\u5b9e\u6ca1\u6709\u4eba\u6392\u5728\u81ea\u5df1\u540e\u9762\r\n                return;\r\n            } else {\r\n                \/\/ \u7a81\u7136\u6709\u4eba\u6392\u5728\u81ea\u5df1\u540e\u9762\u4e86\uff0c\u53ef\u80fd\u8fd8\u4e0d\u77e5\u9053\u662f\u8c01\uff0c\u4e0b\u9762\u662f\u7b49\u5f85\u540e\u7eed\u8005\r\n                \/\/ \u8fd9\u91cc\u4e4b\u6240\u4ee5\u8981\u5fd9\u7b49\u662f\u56e0\u4e3a\uff1astep 1\u6267\u884c\u5b8c\u540e\uff0cstep 2\u53ef\u80fd\u8fd8\u6ca1\u6267\u884c\u5b8c\r\n                while (currentThread.next == null) { \/\/ step 5\r\n                }\r\n            }\r\n        }\r\n\r\n        currentThread.next.isBlock = false;\r\n        currentThread.next = null;\/\/ for GC\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3>CLH\u9501<\/h3>\n<p>CLH\u9501\u4e5f\u662f\u4e00\u79cd\u57fa\u4e8e\u94fe\u8868\u7684\u53ef\u6269\u5c55\u3001\u9ad8\u6027\u80fd\u3001\u516c\u5e73\u7684\u81ea\u65cb\u9501\uff0c\u7533\u8bf7\u7ebf\u7a0b\u53ea\u5728\u672c\u5730\u53d8\u91cf\u4e0a\u81ea\u65cb\uff0c\u5b83\u4e0d\u65ad\u8f6e\u8be2\u524d\u9a71\u7684\u72b6\u6001\uff0c\u5982\u679c\u53d1\u73b0\u524d\u9a71\u91ca\u653e\u4e86\u9501\u5c31\u7ed3\u675f\u81ea\u65cb\u3002<\/p>\n<pre><code class=\"java\">import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;\r\n\r\npublic class CLHLock {\r\n    public static class CLHNode {\r\n        private volatile boolean isLocked = true; \/\/ \u9ed8\u8ba4\u662f\u5728\u7b49\u5f85\u9501\r\n    }\r\n\r\n    @SuppressWarnings(\"unused\" )\r\n    private volatile CLHNode tail ;\r\n    private static final AtomicReferenceFieldUpdater&lt;CLHLock, CLHNode&gt; UPDATER = AtomicReferenceFieldUpdater\r\n                  . newUpdater(CLHLock.class, CLHNode .class , \"tail\" );\r\n\r\n    public void lock(CLHNode currentThread) {\r\n        CLHNode preNode = UPDATER.getAndSet( this, currentThread);\r\n        if(preNode != null) {\/\/\u5df2\u6709\u7ebf\u7a0b\u5360\u7528\u4e86\u9501\uff0c\u8fdb\u5165\u81ea\u65cb\r\n            while(preNode.isLocked ) {\r\n            }\r\n        }\r\n    }\r\n\r\n    public void unlock(CLHNode currentThread) {\r\n        \/\/ \u5982\u679c\u961f\u5217\u91cc\u53ea\u6709\u5f53\u524d\u7ebf\u7a0b\uff0c\u5219\u91ca\u653e\u5bf9\u5f53\u524d\u7ebf\u7a0b\u7684\u5f15\u7528\uff08for GC\uff09\u3002\r\n        if (!UPDATER .compareAndSet(this, currentThread, null)) {\r\n            \/\/ \u8fd8\u6709\u540e\u7eed\u7ebf\u7a0b\r\n            currentThread. isLocked = false ;\/\/ \u6539\u53d8\u72b6\u6001\uff0c\u8ba9\u540e\u7eed\u7ebf\u7a0b\u7ed3\u675f\u81ea\u65cb\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3>CLH\u9501 \u4e0e MCS\u9501 \u7684\u6bd4\u8f83<\/h3>\n<p>\u4e0b\u56fe\u662fCLH\u9501\u548cMCS\u9501\u961f\u5217\u56fe\u793a\uff1a<br \/>\n<img decoding=\"async\" src=\"\/\/coderbee.net\/wp-content\/uploads\/2013\/11\/CLH-MCS-SpinLock.png\" alt=\"CLH-MCS-SpinLock\" \/><\/p>\n<p>\u5dee\u5f02\uff1a<\/p>\n<ol>\n<li>\u4ece\u4ee3\u7801\u5b9e\u73b0\u6765\u770b\uff0cCLH\u6bd4MCS\u8981\u7b80\u5355\u5f97\u591a\u3002<\/li>\n<li>\u4ece\u81ea\u65cb\u7684\u6761\u4ef6\u6765\u770b\uff0cCLH\u662f\u5728\u524d\u9a71\u8282\u70b9\u7684\u5c5e\u6027\u4e0a\u81ea\u65cb\uff0c\u800cMCS\u662f\u5728\u672c\u5730\u5c5e\u6027\u53d8\u91cf\u4e0a\u81ea\u65cb\u3002<\/li>\n<li>\u4ece\u94fe\u8868\u961f\u5217\u6765\u770b\uff0cCLH\u7684\u961f\u5217\u662f\u9690\u5f0f\u7684\uff0cCLHNode\u5e76\u4e0d\u5b9e\u9645\u6301\u6709\u4e0b\u4e00\u4e2a\u8282\u70b9\uff1bMCS\u7684\u961f\u5217\u662f\u7269\u7406\u5b58\u5728\u7684\u3002<\/li>\n<li>CLH\u9501\u91ca\u653e\u65f6\u53ea\u9700\u8981\u6539\u53d8\u81ea\u5df1\u7684\u5c5e\u6027\uff0cMCS\u9501\u91ca\u653e\u5219\u9700\u8981\u6539\u53d8\u540e\u7ee7\u8282\u70b9\u7684\u5c5e\u6027\u3002<\/li>\n<\/ol>\n<p><strong>\u6ce8\u610f\uff1a\u8fd9\u91cc\u5b9e\u73b0\u7684\u9501\u90fd\u662f\u72ec\u5360\u7684\uff0c\u4e14\u4e0d\u80fd\u91cd\u5165\u7684\u3002<\/strong><\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u81ea\u65cb\u9501\uff08Spin lock\uff09 \u81ea\u65cb\u9501\u662f\u6307\u5f53\u4e00\u4e2a\u7ebf\u7a0b\u5c1d\u8bd5\u83b7\u53d6\u67d0\u4e2a\u9501\u65f6\uff0c\u5982\u679c\u8be5\u9501\u5df2\u88ab &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/concurrent\/20131115\/577\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[109,108,104,107,106,105],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/577"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=577"}],"version-history":[{"count":10,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":1752,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/577\/revisions\/1752"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}