

{"id":156,"date":"2021-07-04T23:22:17","date_gmt":"2021-07-04T15:22:17","guid":{"rendered":"https:\/\/www.52dixiaowo.com\/python\/?p=156"},"modified":"2021-07-04T23:29:15","modified_gmt":"2021-07-04T15:29:15","slug":"python%e7%ba%bf%e7%a8%8b%e7%9a%84%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"https:\/\/www.52dixiaowo.com\/python\/post-156.html","title":{"rendered":"python\u7ebf\u7a0b\u7684\u540c\u6b65"},"content":{"rendered":"\n<p>\u7ebf\u7a0b\u4e2d\u7684\u540c\u6b65, \u6307\u7684\u662f\u534f\u8c03\u540c\u6b65, \u6309\u7167\u7ea6\u5b9a\u7684\u5148\u540e\u6b21\u5e8f\u6267\u884c<\/p>\n\n\n\n<h2>\u7ebf\u7a0b\u9501\u7684\u6982\u5ff5<\/h2>\n\n\n\n<p>\u7ebf\u7a0b\u9501, \u80fd\u591f\u628a\u591a\u4e2a\u8bed\u53e5\u5c01\u88c5\u4e3a&#8221;\u4e00\u4e2a\u8bed\u53e5&#8221;, \u4fdd\u8bc1\u64cd\u4f5c\u7684\u539f\u5b50\u6027, \u6bd4\u5982a+=1\u4ee3\u7801\u89e3\u6790\u540e\u6709\u591a\u4e2a\u6b65\u9aa4, \u591a\u7ebf\u7a0b\u5171\u4eab\u65f6\u5c31\u53ef\u80fd\u51fa\u73b0\u9519\u8bef<\/p>\n\n\n\n<p>\u539f\u5b50\u6027\u6307\u7684\u662f, \u8981\u4e48\u4e0d\u505a, \u8981\u4e48\u505a\u5b8c, \u4e0d\u5141\u8bb8\u88ab\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6267\u884c<\/p>\n\n\n\n<p>\u628a\u591a\u7ebf\u7a0b\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801, \u7528\u7ebf\u7a0b\u9501\u9501\u8d77\u6765, \u5c31\u53ef\u4ee5\u907f\u514d\u591a\u7ebf\u7a0b\u5171\u4eab\u53d8\u91cf\u4e0d\u5b89\u5168\u7684\u60c5\u51b5<\/p>\n\n\n\n<h2>python\u7684\u7ebf\u7a0b\u9501<\/h2>\n\n\n\n<p>1.\u521b\u5efa\u7ebf\u7a0b\u9501<\/p>\n\n\n\n<p>\u4f7f\u7528threading.Lock\u65b9\u6cd5\u5f97\u5230\u4e00\u4e2a\u9501, \u5b83\u662f\u4e00\u4e2a\u4e92\u65a5\u9501<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mutex = threading.Lock()<\/code><\/pre>\n\n\n\n<p>2.\u4e0a\u9501<\/p>\n\n\n\n<p>\u8c03\u7528acquire\u65b9\u6cd5\u4e0a\u9501<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mutex.acquire()<\/code><\/pre>\n\n\n\n<p>3.\u89e3\u9501<\/p>\n\n\n\n<p>\u8c03\u7528release\u65b9\u6cd5\u89e3\u9501<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mutex.release()<\/code><\/pre>\n\n\n\n<p>\u628a\u53ef\u80fd\u51fa\u9519\u7684\u4ee3\u7801, \u653e\u5728\u4e0a\u9501\u548c\u89e3\u9501\u4e4b\u95f4<\/p>\n\n\n\n<h2>\u7ebf\u7a0b\u9501\u5b9e\u4f8b<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u4ee3\u7801, 2\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c\u4e00\u4e2a\u53d8\u91cf\u6267\u884c+=1\u64cd\u4f5c, \u5f97\u5230\u7684\u7ed3\u679c\u662f\u6b63\u786e\u7684, \u5982\u679c\u53bb\u6389\u7ebf\u7a0b\u9501, \u5f97\u5230\u7684\u7ed3\u679c\u53ef\u80fd\u6bd4\u9884\u671f\u7684\u5c11<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import threading\nimport time\n\n# \u5b9a\u4e49\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\ngl_num = 0\n\n\ndef test1(num):\n    global gl_num\n    for i in range(num):\n        # \u4e0a\u9501\n        mutex.acquire()\n        gl_num += 1\n        # \u89e3\u9501\n        mutex.release()\n    print(\"test1------\" + str(gl_num))\n\n\ndef test2(num):\n    global gl_num\n    for i in range(num):\n        # \u4e0a\u9501\n        mutex.acquire()\n        gl_num += 1\n        # \u89e3\u9501\n        mutex.release()\n    print(\"test2---\" + str(gl_num))\n\n\n# \u521b\u5efa\u4e00\u4e2a\u9501, \u9ed8\u8ba4\u6ca1\u6709\u4e0a\u9501\nmutex = threading.Lock()\n\n\ndef main():\n    t1 = threading.Thread(target=test1, args=(1000000,))\n    t2 = threading.Thread(target=test2, args=(1000000,))\n\n    t1.start()\n    t2.start()\n\n    # \u7b49\u5f85\u5b50\u7ebf\u7a0b\u8fd0\u884c\u7ed3\u675f\n    time.sleep(3)\n    print(\"main--\" + str(gl_num))\n\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n\n\n\n<p>\u4f60\u9700\u8981\u6ce8\u610f\u7684\u662f, \u7ebf\u7a0b\u9501\u4e0d\u8981\u968f\u610f\u4f7f\u7528, \u591a\u4e2a\u9501\u4e4b\u95f4\u53ef\u80fd\u9020\u6210&#8221;\u6b7b\u9501&#8221;, \u4f8b\u5982<\/p>\n\n\n\n<p>\u5047\u8bbe\u5171\u67092\u4e2a\u9501(x,y), a\u7ebf\u7a0b\u4e0a\u4e86x\u9501, b\u7ebf\u7a0b\u4e0a\u4e86y\u9501, \u800ca\u9700\u8981y\u624d\u80fd\u89e3\u9501, b\u9700\u8981x\u624d\u80fd\u89e3\u9501, \u90a3\u4e48\u663e\u7136a\u65e0\u6cd5\u5f97\u5230y, b\u4e5f\u65e0\u6cd5\u5f97\u5230x, \u5c31\u4f1a\u9020\u6210\u76f8\u4e92\u7b49\u5f85, \u4ece\u800c\u6b7b\u9501\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u7a0b\u4e2d\u7684\u540c\u6b65, \u6307\u7684\u662f\u534f\u8c03\u540c\u6b65, \u6309\u7167\u7ea6\u5b9a\u7684\u5148\u540e\u6b21\u5e8f\u6267\u884c \u7ebf\u7a0b\u9501\u7684\u6982\u5ff5 \u7ebf\u7a0b\u9501, \u80fd\u591f\u628a\u591a\u4e2a\u8bed\u53e5\u5c01\u88c5\u4e3a&#038;#82&hellip; <a href=\"https:\/\/www.52dixiaowo.com\/python\/post-156.html\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">python\u7ebf\u7a0b\u7684\u540c\u6b65<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/156"}],"collection":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":0,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52dixiaowo.com\/python\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}