{"id":10020691,"date":"2025-04-01T11:01:28","date_gmt":"2025-04-01T03:01:28","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020691"},"modified":"2025-04-01T11:01:28","modified_gmt":"2025-04-01T03:01:28","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e7%bc%96%e7%a8%8b%e7%9a%84%e9%9a%90%e5%bd%a2%e9%99%b7%e9%98%b1%ef%bc%9a%e7%ab%9e%e6%80%81%e3%80%81%e6%ad%bb%e9%94%81%e4%b8%8e%e6%b4%bb%e9%94%81%e7%9a%84%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020691","title":{"rendered":"\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u9690\u5f62\u9677\u9631\uff1a\u7ade\u6001\u3001\u6b7b\u9501\u4e0e\u6d3b\u9501\u7684\u5b9e\u6218\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<h2>\u6458\u8981<\/h2>\n<p>\u5e76\u53d1\u7f16\u7a0b\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u4f46\u5b83\u4e5f\u5e26\u6765\u4e86\u8bb8\u591a\u6311\u6218\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u5e38\u89c1\u9677\u9631\uff0c\u5982\u7ade\u6001\u6761\u4ef6\u3001\u6b7b\u9501\u548c\u6d3b\u9501\uff0c\u5206\u6790\u5176\u6210\u56e0\uff0c\u5e76\u63d0\u4f9b\u6709\u6548\u7684\u8c03\u8bd5\u6280\u5de7\u548c\u5de5\u5177\u3002\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u548c\u53ef\u8fd0\u884c\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u907f\u514d\u548c\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u968f\u7740\u591a\u6838\u5904\u7406\u5668\u7684\u666e\u53ca\uff0c\u5e76\u53d1\u7f16\u7a0b\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002\u7136\u800c\uff0c\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u95ee\u9898\u5f80\u5f80\u96be\u4ee5\u53d1\u73b0\u548c\u8c03\u8bd5\u3002\u7ade\u6001\u6761\u4ef6\u3001\u6b7b\u9501\u548c\u6d3b\u9501\u7b49\u95ee\u9898\u4e0d\u4ec5\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\uff0c\u8fd8\u53ef\u80fd\u5bfc\u81f4\u4e25\u91cd\u7684\u6027\u80fd\u95ee\u9898\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u8fd9\u4e9b\u95ee\u9898\u7684\u6210\u56e0\uff0c\u5e76\u63d0\u4f9b\u5b9e\u7528\u7684\u8c03\u8bd5\u6280\u5de7\u548c\u5de5\u5177\u3002<\/p>\n<h2>\u7ade\u6001\u6761\u4ef6<\/h2>\n<h3>\u6210\u56e0<\/h3>\n<p>\u7ade\u6001\u6761\u4ef6\u53d1\u751f\u5728\u591a\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u540c\u65f6\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\uff0c\u4e14\u6700\u7ec8\u7ed3\u679c\u4f9d\u8d56\u4e8e\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u7684\u6267\u884c\u987a\u5e8f\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u53ef\u80fd\u662f\u4e0d\u786e\u5b9a\u7684\u3002<\/p>\n<h3>\u8c03\u8bd5\u6280\u5de7<\/h3>\n<ul>\n<li><strong>\u4f7f\u7528\u9501\u673a\u5236<\/strong>\uff1a\u786e\u4fdd\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u8bbf\u95ee\u662f\u4e92\u65a5\u7684\u3002<\/li>\n<li><strong>\u4f7f\u7528\u539f\u5b50\u64cd\u4f5c<\/strong>\uff1a\u907f\u514d\u5728\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u540c\u65f6\u4fee\u6539\u540c\u4e00\u53d8\u91cf\u3002<\/li>\n<li><strong>\u5de5\u5177<\/strong>\uff1aGDB\u3001Valgrind\u7b49\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u7ade\u6001\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\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        with lock:\n            counter += 1\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>\u6b7b\u9501<\/h2>\n<h3>\u6210\u56e0<\/h3>\n<p>\u6b7b\u9501\u53d1\u751f\u5728\u4e24\u4e2a\u6216\u591a\u4e2a\u7ebf\u7a0b\u4e92\u76f8\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90\uff0c\u5bfc\u81f4\u6240\u6709\u7ebf\u7a0b\u90fd\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u3002<\/p>\n<h3>\u8c03\u8bd5\u6280\u5de7<\/h3>\n<ul>\n<li><strong>\u907f\u514d\u5d4c\u5957\u9501<\/strong>\uff1a\u5c3d\u91cf\u51cf\u5c11\u9501\u7684\u5d4c\u5957\u4f7f\u7528\u3002<\/li>\n<li><strong>\u9501\u7684\u987a\u5e8f<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u83b7\u53d6\u9501\u3002<\/li>\n<li><strong>\u5de5\u5177<\/strong>\uff1aGDB\u3001Valgrind\u7b49\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u6b7b\u9501\u3002<\/li>\n<\/ul>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<pre><code class=\"python\">import threading\n\nlock1 = threading.Lock()\nlock2 = threading.Lock()\n\ndef thread1():\n    with lock1:\n        with lock2:\n            print(&quot;Thread 1&quot;)\n\ndef thread2():\n    with lock2:\n        with lock1:\n            print(&quot;Thread 2&quot;)\n\nt1 = threading.Thread(target=thread1)\nt2 = threading.Thread(target=thread2)\n\nt1.start()\nt2.start()\n\nt1.join()\nt2.join()<\/code><\/pre>\n<h2>\u6d3b\u9501<\/h2>\n<h3>\u6210\u56e0<\/h3>\n<p>\u6d3b\u9501\u53d1\u751f\u5728\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u4e0d\u65ad\u5c1d\u8bd5\u89e3\u51b3\u51b2\u7a81\uff0c\u4f46\u59cb\u7ec8\u65e0\u6cd5\u53d6\u5f97\u8fdb\u5c55\u3002\u4e0e\u6b7b\u9501\u4e0d\u540c\uff0c\u6d3b\u9501\u4e2d\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u4ecd\u5728\u8fd0\u884c\uff0c\u4f46\u65e0\u6cd5\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<h3>\u8c03\u8bd5\u6280\u5de7<\/h3>\n<ul>\n<li><strong>\u5f15\u5165\u968f\u673a\u6027<\/strong>\uff1a\u5728\u91cd\u8bd5\u673a\u5236\u4e2d\u5f15\u5165\u968f\u673a\u5ef6\u8fdf\uff0c\u907f\u514d\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u540c\u6b65\u3002<\/li>\n<li><strong>\u8d44\u6e90\u5206\u914d\u7b56\u7565<\/strong>\uff1a\u4f18\u5316\u8d44\u6e90\u5206\u914d\u7b56\u7565\uff0c\u51cf\u5c11\u51b2\u7a81\u3002<\/li>\n<li><strong>\u5de5\u5177<\/strong>\uff1aGDB\u3001Valgrind\u7b49\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u6d3b\u9501\u3002<\/li>\n<\/ul>\n<h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<pre><code class=\"python\">import threading\nimport time\nimport random\n\nlock1 = threading.Lock()\nlock2 = threading.Lock()\n\ndef thread1():\n    while True:\n        with lock1:\n            time.sleep(random.random())\n            if lock2.acquire(blocking=False):\n                print(&quot;Thread 1&quot;)\n                lock2.release()\n                break\n            else:\n                lock1.release()\n                time.sleep(random.random())\n\ndef thread2():\n    while True:\n        with lock2:\n            time.sleep(random.random())\n            if lock1.acquire(blocking=False):\n                print(&quot;Thread 2&quot;)\n                lock1.release()\n                break\n            else:\n                lock2.release()\n                time.sleep(random.random())\n\nt1 = threading.Thread(target=thread1)\nt2 = threading.Thread(target=thread2)\n\nt1.start()\nt2.start()\n\nt1.join()\nt2.join()<\/code><\/pre>\n<h2>QA\u73af\u8282<\/h2>\n<h3>Q: \u5982\u4f55\u907f\u514d\u7ade\u6001\u6761\u4ef6\uff1f<\/h3>\n<p>A: \u4f7f\u7528\u9501\u673a\u5236\u6216\u539f\u5b50\u64cd\u4f5c\u6765\u786e\u4fdd\u5bf9\u5171\u4eab\u8d44\u6e90\u7684\u4e92\u65a5\u8bbf\u95ee\u3002<\/p>\n<h3>Q: \u5982\u4f55\u68c0\u6d4b\u6b7b\u9501\uff1f<\/h3>\n<p>A: \u4f7f\u7528GDB\u3001Valgrind\u7b49\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u68c0\u6d4b\u6b7b\u9501\u3002<\/p>\n<h3>Q: \u6d3b\u9501\u548c\u6b7b\u9501\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n<p>A: \u6b7b\u9501\u4e2d\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u5b8c\u5168\u505c\u6b62\u8fd0\u884c\uff0c\u800c\u6d3b\u9501\u4e2d\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u4ecd\u5728\u8fd0\u884c\uff0c\u4f46\u65e0\u6cd5\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u9677\u9631\u5982\u7ade\u6001\u6761\u4ef6\u3001\u6b7b\u9501\u548c\u6d3b\u9501\u662f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u89c1\u7684\u95ee\u9898\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u95ee\u9898\u7684\u6210\u56e0\uff0c\u5e76\u4f7f\u7528\u9002\u5f53\u7684\u8c03\u8bd5\u6280\u5de7\u548c\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u907f\u514d\u548c\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<p>\u968f\u7740\u5e76\u53d1\u7f16\u7a0b\u7684\u590d\u6742\u6027\u4e0d\u65ad\u589e\u52a0\uff0c\u672a\u6765\u7684\u7814\u7a76\u5c06\u66f4\u52a0\u6ce8\u91cd\u81ea\u52a8\u5316\u5de5\u5177\u7684\u5f00\u53d1\uff0c\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u8f7b\u677e\u5730\u68c0\u6d4b\u548c\u89e3\u51b3\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li>\u300aJava\u5e76\u53d1\u7f16\u7a0b\u5b9e\u6218\u300b<\/li>\n<li>\u300a\u64cd\u4f5c\u7cfb\u7edf\u6982\u5ff5\u300b<\/li>\n<li>GDB\u5b98\u65b9\u6587\u6863<\/li>\n<li>Valgrind\u5b98\u65b9\u6587\u6863<\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046402128\">https:\/\/segmentfault.com\/a\/1190000046402128<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u5e76\u53d1\u7f16\u7a0b\u662f\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u4f46\u5b83\u4e5f\u5e26\u6765\u4e86\u8bb8\u591a\u6311\u6218\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5e76\u53d1\u7f16\u7a0b\u4e2d\u7684\u5e38\u89c1\u9677\u9631\uff0c\u5982\u7ade\u6001\u6761 [&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-10020691","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\/10020691","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=10020691"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020691\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}