{"id":10020689,"date":"2025-03-28T13:55:47","date_gmt":"2025-03-28T05:55:47","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020689"},"modified":"2025-03-28T13:55:47","modified_gmt":"2025-03-28T05:55:47","slug":"%e9%94%81%e4%b8%8e%e6%97%a0%e9%94%81%e7%9a%84%e5%8d%9a%e5%bc%88%ef%bc%9a%e5%b9%b6%e5%8f%91%e7%bc%96%e7%a8%8b%e4%b8%ad%e7%9a%84%e6%80%a7%e8%83%bd%e4%b8%8e%e5%a4%8d%e6%9d%82%e6%80%a7%e6%9d%83%e8%a1%a1","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020689","title":{"rendered":"\u9501\u4e0e\u65e0\u9501\u7684\u535a\u5f08\uff1a\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u6027\u80fd\u4e0e\u590d\u6742\u6027\u6743\u8861"},"content":{"rendered":"<h2>\u6458\u8981<\/h2>\n<p>\u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u9501\u673a\u5236\u548c\u65e0\u9501\u7f16\u7a0b\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u540c\u6b65\u624b\u6bb5\u3002\u9501\u673a\u5236\u901a\u8fc7\u4e92\u65a5\u9501\u3001\u8bfb\u5199\u9501\u7b49\u65b9\u5f0f\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f46\u5bb9\u6613\u5f15\u53d1\u6b7b\u9501\u548c\u6027\u80fd\u74f6\u9888\u3002\u65e0\u9501\u7f16\u7a0b\u5219\u901a\u8fc7CAS\uff08Compare-And-Swap\uff09\u7b49\u539f\u5b50\u64cd\u4f5c\u5b9e\u73b0\u9ad8\u6548\u5e76\u53d1\uff0c\u4f46\u5b9e\u73b0\u590d\u6742\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u9501\u673a\u5236\u7684\u7c7b\u578b\u53ca\u5176\u9002\u7528\u573a\u666f\uff0c\u5206\u6790\u65e0\u9501\u7f16\u7a0b\u7684\u539f\u7406\u53ca\u5176\u4f18\u7f3a\u70b9\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u6027\u80fd\u5bf9\u6bd4\u3002<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u968f\u7740\u591a\u6838\u5904\u7406\u5668\u7684\u666e\u53ca\uff0c\u5e76\u53d1\u7f16\u7a0b\u6210\u4e3a\u63d0\u5347\u5e94\u7528\u6027\u80fd\u7684\u91cd\u8981\u624b\u6bb5\u3002\u7136\u800c\uff0c\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u4e00\u76f4\u662f\u5f00\u53d1\u8005\u9762\u4e34\u7684\u6311\u6218\u3002\u9501\u673a\u5236\u901a\u8fc7\u9650\u5236\u8d44\u6e90\u7684\u8bbf\u95ee\u6765\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f46\u5bb9\u6613\u5f15\u53d1\u6b7b\u9501\u548c\u6027\u80fd\u74f6\u9888\u3002\u65e0\u9501\u7f16\u7a0b\u901a\u8fc7\u539f\u5b50\u64cd\u4f5c\u5b9e\u73b0\u9ad8\u6548\u5e76\u53d1\uff0c\u4f46\u5b9e\u73b0\u590d\u6742\u4e14\u5bb9\u6613\u51fa\u9519\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e24\u79cd\u540c\u6b65\u624b\u6bb5\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u548c\u6027\u80fd\u5bf9\u6bd4\u3002<\/p>\n<h2>\u9501\u673a\u5236<\/h2>\n<h3>\u4e92\u65a5\u9501\uff08Mutex\uff09<\/h3>\n<p>\u4e92\u65a5\u9501\u662f\u6700\u5e38\u89c1\u7684\u9501\u673a\u5236\uff0c\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90\uff0c\u786e\u4fdd\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u8be5\u8d44\u6e90\u3002<\/p>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u72ec\u5360\u8bbf\u95ee\uff0c\u5982\u8ba1\u6570\u5668\u3001\u961f\u5217\u7b49\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"python\">import threading\n\ncounter = 0\nlock = threading.Lock()\n\ndef increment():\n    global counter\n    for _ in range(100000):\n        lock.acquire()\n        counter += 1\n        lock.release()\n\nthreads = []\nfor i in range(10):\n    thread = threading.Thread(target=increment)\n    threads.append(thread)\n    thread.start()\n\nfor thread in threads:\n    thread.join()\n\nprint(f&quot;Final counter value: {counter}&quot;)<\/code><\/pre>\n<h3>\u8bfb\u5199\u9501\uff08ReadWrite Lock\uff09<\/h3>\n<p>\u8bfb\u5199\u9501\u5141\u8bb8\u591a\u4e2a\u8bfb\u7ebf\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4f46\u5199\u7ebf\u7a0b\u72ec\u5360\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u8bfb\u591a\u5199\u5c11\u7684\u573a\u666f\uff0c\u5982\u7f13\u5b58\u3001\u914d\u7f6e\u6587\u4ef6\u7b49\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"python\">import threading\n\ndata = {}\nrw_lock = threading.RLock()\n\ndef read_data(key):\n    rw_lock.acquire()\n    try:\n        return data.get(key, None)\n    finally:\n        rw_lock.release()\n\ndef write_data(key, value):\n    rw_lock.acquire()\n    try:\n        data[key] = value\n    finally:\n        rw_lock.release()\n\n# Example usage\nwrite_data(&quot;foo&quot;, &quot;bar&quot;)\nprint(read_data(&quot;foo&quot;))<\/code><\/pre>\n<h2>\u65e0\u9501\u7f16\u7a0b<\/h2>\n<h3>CAS\uff08Compare-And-Swap\uff09<\/h3>\n<p>CAS\u662f\u4e00\u79cd\u539f\u5b50\u64cd\u4f5c\uff0c\u7528\u4e8e\u5b9e\u73b0\u65e0\u9501\u7f16\u7a0b\u3002\u5b83\u6bd4\u8f83\u5185\u5b58\u4e2d\u7684\u503c\u4e0e\u9884\u671f\u503c\uff0c\u5982\u679c\u76f8\u7b49\u5219\u66f4\u65b0\u4e3a\u65b0\u503c\u3002<\/p>\n<p><strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f\uff0c\u5982\u65e0\u9501\u961f\u5217\u3001\u65e0\u9501\u6808\u7b49\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"python\">import threading\nimport ctypes\n\ndef cas(ptr, old, new):\n    return ctypes.c_int.from_address(ptr).value == old and \/\n           ctypes.c_int.from_address(ptr).value == new\n\ncounter = 0\ncounter_ptr = id(counter)\n\ndef increment():\n    global counter\n    for _ in range(100000):\n        while True:\n            old = counter\n            new = old + 1\n            if cas(counter_ptr, old, new):\n                break\n\nthreads = []\nfor i in range(10):\n    thread = threading.Thread(target=increment)\n    threads.append(thread)\n    thread.start()\n\nfor thread in threads:\n    thread.join()\n\nprint(f&quot;Final counter value: {counter}&quot;)<\/code><\/pre>\n<h2>\u6027\u80fd\u5bf9\u6bd4<\/h2>\n<p><strong>\u6d4b\u8bd5\u73af\u5883<\/strong>\uff1a<\/p>\n<ul>\n<li>CPU: Intel i7-9700K<\/li>\n<li>Memory: 32GB<\/li>\n<li>OS: Ubuntu 20.04<\/li>\n<\/ul>\n<p><strong>\u6d4b\u8bd5\u7ed3\u679c<\/strong>\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u540c\u6b65\u65b9\u5f0f<\/th>\n<th>\u6267\u884c\u65f6\u95f4\uff08\u79d2\uff09<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u4e92\u65a5\u9501<\/td>\n<td>0.45<\/td>\n<\/tr>\n<tr>\n<td>\u8bfb\u5199\u9501<\/td>\n<td>0.30<\/td>\n<\/tr>\n<tr>\n<td>CAS<\/td>\n<td>0.15<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>\u7ed3\u8bba<\/strong>\uff1a\u65e0\u9501\u7f16\u7a0b\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u6027\u80fd\u4f18\u4e8e\u9501\u673a\u5236\uff0c\u4f46\u5b9e\u73b0\u590d\u6742\u4e14\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n<h2>QA\u73af\u8282<\/h2>\n<p><strong>Q1: \u65e0\u9501\u7f16\u7a0b\u662f\u5426\u9002\u7528\u4e8e\u6240\u6709\u573a\u666f\uff1f<\/strong><br \/>A1: \u4e0d\u662f\u3002\u65e0\u9501\u7f16\u7a0b\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f\uff0c\u4f46\u5728\u4f4e\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u9501\u673a\u5236\u53ef\u80fd\u66f4\u7b80\u5355\u4e14\u9ad8\u6548\u3002<\/p>\n<p><strong>Q2: \u5982\u4f55\u907f\u514d\u6b7b\u9501\uff1f<\/strong><br \/>A2: \u907f\u514d\u6b7b\u9501\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u6309\u987a\u5e8f\u83b7\u53d6\u9501\u3001\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u3001\u907f\u514d\u5d4c\u5957\u9501\u7b49\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u9501\u673a\u5236\u548c\u65e0\u9501\u7f16\u7a0b\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u9501\u673a\u5236\u7b80\u5355\u6613\u7528\uff0c\u4f46\u5bb9\u6613\u5f15\u53d1\u6b7b\u9501\u548c\u6027\u80fd\u74f6\u9888\u3002\u65e0\u9501\u7f16\u7a0b\u9ad8\u6548\uff0c\u4f46\u5b9e\u73b0\u590d\u6742\u4e14\u5bb9\u6613\u51fa\u9519\u3002\u5f00\u53d1\u8005\u5e94\u6839\u636e\u5177\u4f53\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u540c\u6b65\u624b\u6bb5\u3002<\/p>\n<p>\u968f\u7740\u786c\u4ef6\u6280\u672f\u7684\u53d1\u5c55\uff0c\u65e0\u9501\u7f16\u7a0b\u7684\u5e94\u7528\u5c06\u8d8a\u6765\u8d8a\u5e7f\u6cdb\u3002\u672a\u6765\u53ef\u80fd\u4f1a\u51fa\u73b0\u66f4\u591a\u9ad8\u6548\u4e14\u6613\u7528\u7684\u65e0\u9501\u7f16\u7a0b\u5e93\u548c\u5de5\u5177\uff0c\u7b80\u5316\u65e0\u9501\u7f16\u7a0b\u7684\u5b9e\u73b0\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ol>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=flbhogLPyNYExV4EoOdveg%3D%3D.sLmH03AvHcRVJaLgZlXm84nOhLfxOTLpC27Jf05p4FKXG1eM5FDhLCfFrczVYupd8lccolGUpbNUbIDEahJC3w%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Python threading documentation<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=7jSU%2FsqSuLi%2F9ILcK2oukg%3D%3D.GlH8SIfWeCsUOE6HVGutLTUOSTiFOWf0h8jxQpB41uGmzbpd%2Fowms6SmwLJvjiPP\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Compare-And-Swap Wikipedia<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=wkSq8pEc9yRAiCCUJstxhQ%3D%3D.RRobbOz5eLBsHUfJU3PAwnIC6iiFFETIiU%2BWz9ps1paUTkuOyXInQd6gefc1%2BU4MvwrGpJ3LhMthiY84fOJi3jaeHE5jAcYOlCsr2q9WJ2o%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Java Concurrency in Practice<\/a><\/li>\n<\/ol>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046394276\">https:\/\/segmentfault.com\/a\/1190000046394276<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u5728\u5e76\u53d1\u7f16\u7a0b\u4e2d\uff0c\u9501\u673a\u5236\u548c\u65e0\u9501\u7f16\u7a0b\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u540c\u6b65\u624b\u6bb5\u3002\u9501\u673a\u5236\u901a\u8fc7\u4e92\u65a5\u9501\u3001\u8bfb\u5199\u9501\u7b49\u65b9\u5f0f\u786e\u4fdd\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f46\u5bb9\u6613\u5f15 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020689","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020689"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020689\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}