{"id":1174,"date":"2024-04-13T15:22:51","date_gmt":"2024-04-13T07:22:51","guid":{"rendered":"https:\/\/imuslab.com\/wordpress\/?p=1174"},"modified":"2024-04-13T15:55:13","modified_gmt":"2024-04-13T07:55:13","slug":"concurrent-racing-conditions-%ef%bc%88%e4%b8%a6%e7%99%bc%e8%88%87%e7%ab%b6%e6%85%8b%e6%a2%9d%e4%bb%b6%ef%bc%89","status":"publish","type":"post","link":"https:\/\/imuslab.com\/wordpress\/2024\/04\/13\/concurrent-racing-conditions-%ef%bc%88%e4%b8%a6%e7%99%bc%e8%88%87%e7%ab%b6%e6%85%8b%e6%a2%9d%e4%bb%b6%ef%bc%89\/","title":{"rendered":"Concurrent &#038; Racing Conditions \uff08\u4e26\u767c\u8207\u7af6\u614b\u689d\u4ef6\uff09"},"content":{"rendered":"\n<p>\u6700\u8fd1\u56e0\u70ba\u6211\u5728\u958b\u767c Zoraxy \u6642\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u53ef\u4ee5\u505a\u5230 high speed concurrent READ \u8ddf\u9593\u4e2d write \u7684 map\uff0c\u8ddf\u53e6\u4e00\u4f4d\u9ad8\u96c4\u7684\u5de5\u7a0b\u5e2b\u5927\u4f6c\u8a0e\u8ad6\u4e86\u4e00\u500b\u665a\u4e0a\uff0c\u5f8c\u4f86\u6211\u767c\u73fe\u539f\u4f86\u6eff\u591a\u4eba\u5c0d concurrent \/ racing conditions \u7684\u8655\u7406\u6709\u4e00\u5b9a\u7a0b\u5ea6\u7684\u8aa4\u89e3\uff0c\u6240\u4ee5\u6211\u5c31\u4f86\u7c21\u55ae\u7684\u89e3\u91cb\u4e00\u4e0b\u6700\u5e38\u898b\u7684\u8aa4\u89e3 &#8211; Check for locking\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u554f\u984c<\/h2>\n\n\n\n<p>\u5047\u8a2d\u4f60\u6709\u5169\u500b process\uff0c\u4e00\u908a\u8981\u8b80\u53d6\u4e00\u500b\u7121\u6cd5\u540c\u6642\u8b80\u5beb\u7684 map \u8cc7\u6599\u7d50\u69cb\uff08\u4f8b\u5982 Go \u7684 map[string]struct{}\uff09\uff0c\u53e6\u4e00\u500b\u8981\u5beb\u5165\u65b0\u7684 key \u5230\u540c\u4e00\u500b map\u3002\u7531\u65bc map \u4e26\u6c92\u8fa6\u6cd5\u8655\u7406 concurrent R\/W\uff0c\u6240\u4ee5\u4f60\u8981\u600e\u6a23\u505a\u5462\uff1f<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u6c7a\u65b9\u6cd5 1\uff1a\u6539\u7528\u5c08\u7528\u7684 concurrent map \uff08\u5982 Go \u7684 sync.Map\uff09<\/h3>\n\n\n\n<p>\u9019\u500b\u89e3\u6c7a\u65b9\u6cd5\u7c21\u55ae\u76f4\u63a5\uff0c\u6211\u5c31\u4e0d\u518d\u8a73\u7d30\u89e3\u91cb\u4e86\u3002\u7136\u800c\u7531\u65bc\u6211\u5728\u958b\u767c Zoraxy \u7684 high concurrent access \u7279\u8cea\uff0c\u4f7f\u7528 sync.Map \u7684\u8a71\u6703\u505a\u6210\u5f88\u9ad8\u7684 over-head\uff0c\u56e0\u6b64\u6211\u4e26\u4e0d\u662f\u5f88\u60f3\u7528\u5b83\uff08\u4f46\u662f\u4e5f\u4e0d\u5f71\u97ff sync.Map \u4f5c\u70ba\u6c92\u6cd5\u89e3\u6c7a\u6642\u7528\u7684\u6700\u5f8c\u65b9\u6848\u7684\u6c7a\u5b9a\uff09<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u89e3\u6c7a\u65b9\u6cd5 2\uff1amutex lock<\/h3>\n\n\n\n<p>\u9019\u7b97\u662f\u6bd4\u8f03\u6b63\u898f\u7684\u65b9\u6848\u4e4b\u4e00\u3002\u5728\u8b80\u53d6\u548c\u5beb\u5165\u4e4b\u524d\u5148\u5c0d map (\u6216\u662f slice) \u9032\u884c\u9396\u5b9a\uff0c\u7136\u5f8c\u5728\u5beb\u5165\u6216\u8b80\u53d6\u4e4b\u5f8c\u89e3\u9396\u3002\u9019\u6a23\u5c31\u80fd\u78ba\u4fdd\u540c\u6642\u9593\u53ea\u6709\u4e00\u500b process \u5c0d\u9019\u500b map \u6709 R\/W access\u3002\u4f46\u662f\u9019\u540c\u6642\u4e5f\u6703\u7522\u751f\u53e6\u4e00\u500b\u554f\u984c\uff0c\u5c31\u662f\u5728 read lock \u4e86\u4e4b\u5f8c\uff0c\u5047\u8a2d\u6709\u53e6\u4e00\u500b process \u4e5f\u60f3\u540c\u6642\u9032\u884c\u8b80\u53d6\u7684\u8a71\u5c31\u5fc5\u9808\u8981\u7b49\u524d\u9762\u7684\u8b80\u5b8c\uff0c\u56e0\u6b64\u9019\u500b\u65b9\u6848\u5c0d\u65bc\u9ad8\u4e26\u767c\u7684 request handling \u4f86\u8aaa\u4e26\u4e0d\u662f\u592a\u6709\u6548\u7387\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u90a3\u807d\u5230\u9019\u88e1\u7684\u6642\u5019\u5f88\u591a\u4eba\u6703\u76f4\u89ba\u7684\u89ba\u5f97\uff1a\u90a3\u53ea\u6709 write \u7684\u6642\u5019 lock \u5c31\u597d\u5566\uff0cread \u7684\u6642\u5019\u6aa2\u67e5 map \u662f\u4e0d\u662f\u88ab lock\uff0c\u6c92 lock \u7684\u8a71\u5c31\u8b80\u53d6\u3002\u7531\u65bc\u6aa2\u67e5\u7684\u6642\u5019\u4e0d\u6703 lock\uff0c\u56e0\u6b64\u5176\u4ed6\u9700\u8981 read \u7684 process \u4e5f\u4e0d\u6703\u88ab\u64cb\u4e0d\u662f\u55ce\uff1f<\/h4>\n\n\n\n<p>\u807d\u4e0a\u53bb\u597d\u50cf\u6c92\u932f\u5594\uff1f\u8b93\u6211\u7c21\u55ae\u5beb\u4e00\u500b pseudocode \u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function write(){\n    mutex.lock()\n    map.write(\"foo\", \"bar\")\n    mutex.unlock()\n}\n\nfunction read(){\n    if (!mutex.isLocked()){\n        \/\/Map is not locked\n        var value = map.read(\"foo\")\n        print(value)\n    }else{\n      \/\/Map is locked, retry after 100ms\n      delay(100)\n      read()\n    }\n}<\/code><\/pre>\n\n\n\n<p>mutex \u7684\u90e8\u5206\u53ef\u4ee5\u662f programming language \u81ea\u5e36\u7684 mutex \u529f\u80fd\u6216\u662f\u7c21\u55ae\u7684 boolean\uff0c\u6548\u679c\u90fd\u662f\u4e00\u6a23\u7684\u3002\u4f46\u662f\u5982\u679c\u4f60\u6709\u6ce8\u610f\u5230\uff0c\u4f60\u4e0d\u6703\u627e\u5230 mutex.isLocked() \u9019\u500b\u529f\u80fd\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"439\" src=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/\u5716\u7247-2-1024x439.png\" alt=\"\" class=\"wp-image-1175\" srcset=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/\u5716\u7247-2-1024x439.png 1024w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/\u5716\u7247-2-300x129.png 300w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/\u5716\u7247-2-768x329.png 768w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/\u5716\u7247-2-1200x515.png 1200w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/\u5716\u7247-2.png 1257w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u5982\u679c\u4f60\u9700\u8981\u7528\u5230\u9019\u500b\uff0c\u5f88\u5927\u6a5f\u6703\u4f60\u5beb\u932f\u4e86<\/figcaption><\/figure>\n\n\n\n<p>\u9019\u662f\u70ba\u751a\u9ebc\u5462\uff1f\u90a3\u662f\u56e0\u70ba OS \u5c64\u57fa\u672c\u904b\u4f5c\u539f\u7406\u6240\u5f15\u8d77\u7684\u3002\u9019\u88e1\u5c0d\u65bc\u672c\u79d1\u6c92\u4fee Computer Architecture \uff08\u8a08\u7b97\u6a5f\u67b6\u69cb\uff09\u7684\u5de5\u7a0b\u5e2b\u53ef\u80fd\u6bd4\u8f03\u96e3\u7406\u89e3\u3002\u7c21\u55ae\u4f86\u8aaa\u5c31\u662f\uff0cCPU \u4e0a\u5c31\u53ea\u6709 n \u500b\u6838\u5fc3\uff0c\u4f46\u662f\u4f60\u7684\u4f5c\u696d\u7cfb\u7d71\u4e0a\u9762\u6709\u5f88\u591a\u6771\u897f\u5728\u57f7\u884c\uff0c\u6240\u4ee5 OS \u6703\u5c0d\u4e0a\u9762\u57f7\u884c\u7684\u5de5\u4f5c\uff08process\uff09\u9032\u884c\u5feb\u901f\u5207\u63db\u3002\u6bcf\u6b21\u5207\u63db\u7684\u6642\u5019\u56e0\u70ba\u901f\u5ea6\u5f88\u5feb\uff0c\u6240\u4ee5\u6703\u8b93\u4f60\u6709\u4e00\u7a2e\u5b83\u5011\u5728\u4e26\u884c\u8655\u7406\u7684\u611f\u89ba\u3002\u800c\u5be6\u8cea\u4e0a\uff0c\u6bcf\u500b process switching \u90fd\u6703\u6709\u4e00\u500b overhead\uff0c\u800c\u9019\u500b overhead \u5c31\u662f\u70ba\u751a\u9ebc mutex lock \u6c92\u8fa6\u6cd5\u88ab\u7528\u65bc\u6aa2\u67e5\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"398\" src=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/Round_Robin_Schedule_Example-1024x398.jpg\" alt=\"\" class=\"wp-image-1176\" srcset=\"https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/Round_Robin_Schedule_Example-1024x398.jpg 1024w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/Round_Robin_Schedule_Example-300x117.jpg 300w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/Round_Robin_Schedule_Example-768x299.jpg 768w, https:\/\/imuslab.com\/wordpress\/wp-content\/uploads\/2024\/04\/Round_Robin_Schedule_Example.jpg 1082w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>\u5176\u4e2d\u4e00\u7a2e\u5e38\u7528\u7684 scheduling \u6f14\u7b97\u6cd5\uff1aRound Robin Process Switching<\/figcaption><\/figure>\n\n\n\n<p>\u95dc\u65bc CPU scheduling \u8ddf scheduling \u6f14\u7b97\u6cd5\u7684\u8a73\u60c5\u53ef\u4ee5\u770b<a rel=\"noreferrer noopener\" aria-label=\"\u9019\u908a (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Round-robin_scheduling\" target=\"_blank\">\u9019\u908a<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6240\u4ee5\u70ba\u751a\u9ebc Mutex \u4e0d\u80fd\u6aa2\u67e5\u662f\u4e0d\u662f\u88ab\u5176\u4ed6 process \u9396\u4e0a\uff1f<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">\u7576\u4f60\u53bb\u6aa2\u67e5\u4e00\u500b mutex \u662f\u5426\u88ab\u9396\u7684\u6642\u5019\uff0c\u6aa2\u67e5\u7576\u4e0b\u53ef\u80fd\u6c92\u88ab\u9396\uff0c\u4f46\u662f\u6aa2\u67e5\u5b8c\u5230\u57f7\u884c\u4e0b\u4e00\u500b\u908f\u8f2f\u7684\u6642\u5019\u53ef\u80fd\u5df2\u7d93\u88ab\u53e6\u4e00\u500b process \u9396\u4e0a\u4e86<\/h3>\n\n\n\n<p>\u4ee5 compile language \u70ba\u4f8b\uff0c\u4f60\u7684 if then else case \u5728 assembly \u4e0b\u5176\u5be6\u662f\u4e00\u53e5 conditional jump \u6307\u4ee4\u3002\u5f88\u591a\u6642\u5019\uff0c\u5c31\u5982\u4e0a\u5716 round robin \u6240\u986f\u793a\u7684\uff0c\u9019\u53e5 byte code \u6703\u7e7c\u7e8c\u591a\u57f7\u884c\u5e7e\u53e5 byte code \u624d\u6703\u63db\u53e6\u4e00\u500b process \u7684 stack pointer\uff08\u5373\u662f\u5207\u63db\u5230\u53e6\u4e00\u500b process\uff09\uff0c\u4f46\u662f\u5728\u9ad8\u4e26\u767c\u74b0\u5883\u4e0b\uff0c CPU \u6709\u5f88\u5927\u6a5f\u6703\u5728\u57f7\u884c\u5b8c if-then-else check \u662f\u4e0d\u662f mutex lock \u4e4b\u5f8c\u5c31\u6703\u5207\u63db\u5230\u4e0b\u4e00\u500b process\u3002\u800c\u5982\u679c\u525b\u597d\u90a3\u500b process \u628a\u5b83 lock \u4e86\uff0c\u4f60\u5148\u524d\u6aa2\u67e5\u7684 lock state \u5c31\u4e0d\u9069\u7528\u4e86\uff0c\u7e7c\u800c\u5c0e\u81f4 runtime panic\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u7528 check for mutex lock \u9019\u7a2e coding logic \u5728 production \u74b0\u5883\u4e0b\u7528\u662f\u975e\u5e38\u5371\u96aa\u7684\u3002\u56e0\u70ba\u9019\u7a2e code \u5beb\u51fa\u4f86\u5f8c\uff0c\u5f88\u591a\u6642\u5019\u5728 development \u74b0\u5883 \/ low load \u74b0\u5883\u4e0b\u662f\u80fd\u7528\u800c\u4e14\u4e5f\u80fd\u901a\u904e compiler\uff0c\u53ef\u662f\u5230\u4e86 production \u7684\u74b0\u5883 \/ high load \u74b0\u5883\u4e0b\u70b8\u6389\u3002\u5982\u679c\u4f60\u5c0d concurrent access \u6982\u5ff5\u4e0d\u719f\u7684\u8a71\u4f60\u5c31\u53ea\u80fd\u82b1\u5e7e\u5929\u5f9e stack trace \u88e1\u9762\u6162\u6162\u628a\u539f\u56e0\u7ffb\u51fa\u4f86\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5f8c\u4f86\u7684\u89e3\u6c7a\u65b9\u6cd5<\/h3>\n\n\n\n<p>\u5f8c\u4f86\u6211\u6539\u7528 pointer \u7684\u65b9\u5f0f\uff0c\u5728\u9700\u8981\u5beb\u5165\u7684\u6642\u5019\u5148\u5c0d\u539f\u5148\u7684 map \u9032\u884c deep copy\uff0c\u7136\u5f8c\u628a\u6539\u597d\u7684\u65b0 map \u4ee5\u6539 pointer \u7684\u65b9\u5f0f\u5beb\u5165\u5230\u539f\u5148\u7684 struct \u88e1\u9762\u3002\u53e6\u5916\u5728 READ \u908f\u8f2f\u88e1\u9762\uff0c\u6211\u4e5f\u6703\u5148\u628a pointer \u6307\u5411\u7684 map \u5148\u5beb\u5230\u4e00\u500b\u65b0\u7684 local scope pointer variable \u88e1\uff0c\u7136\u5f8c\u5728\u5f8c\u9762\u7684 READ \u908f\u8f2f\uff08\u5982 for loop\uff09\u90fd\u4f7f\u7528\u4e00\u958b\u59cb\u8907\u88fd\u4e0b\u4f86\u7684 pointer\u3002\u9019\u6a23\u5373\u4f7f\u662f\u5728 READ \u7684\u6642\u5019\u88ab\u5beb\u5165\uff0cREAD \u7684\u908f\u8f2f\u4e5f\u6703\u4fdd\u6301\u4f7f\u7528\u820a\u7684 map \u3002\u5f85 READ \u908f\u8f2f\u5b8c\u6210\u4e4b\u5f8c\uff0c\u820a map \u5c31\u6703\u81ea\u7136\u88ab GC \uff08Garbage Collection\uff09\u5f9e memory \u8655\u7406\u6389\u3002<\/p>\n\n\n\n<p>\u5c31\u9019\u6a23\uff0c\u6211\u5011\u5c31\u505a\u597d\u4e00\u500b\u80fd concurrent R\/W \u7684\uff08\u985e\u4f3c\uff09 map \u7684\u65b9\u6848\u5566~ <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u56e0\u70ba\u6211\u5728\u958b\u767c Zoraxy \u6642\u9700\u8981\u8a2d\u8a08\u4e00\u500b\u53ef\u4ee5\u505a\u5230 high speed concurrent READ \u8ddf\u9593\u4e2d write \u7684 map\uff0c\u8ddf\u53e6\u4e00\u4f4d\u9ad8\u96c4\u7684\u5de5\u7a0b\u5e2b\u5927\u4f6c\u8a0e\u8ad6\u4e86\u4e00\u500b\u665a\u4e0a\uff0c\u5f8c\u4f86\u6211\u767c\u73fe\u539f\u4f86\u6eff\u591a\u4eba\u5c0d concurrent \/ racing conditions \u7684\u8655\u7406\u6709\u4e00\u5b9a\u7a0b\u5ea6\u7684\u8aa4\u89e3\uff0c\u6240\u4ee5\u6211\u5c31\u4f86\u7c21\u55ae\u7684\u89e3\u91cb\u4e00\u4e0b\u6700\u5e38\u898b\u7684\u8aa4\u89e3 &#8211; Check for locking\u3002 \u554f\u984c \u5047\u8a2d\u4f60\u6709\u5169\u500b process\uff0c\u4e00\u908a\u8981\u8b80\u53d6\u4e00\u500b\u7121\u6cd5\u540c\u6642\u8b80\u5beb\u7684 map \u8cc7\u6599\u7d50\u69cb\uff08\u4f8b\u5982 Go \u7684 map[string]struct{}\uff09\uff0c\u53e6\u4e00\u500b\u8981\u5beb\u5165\u65b0\u7684 key \u5230\u540c\u4e00\u500b map\u3002\u7531\u65bc map \u4e26\u6c92\u8fa6\u6cd5\u8655\u7406 concurrent R\/W\uff0c\u6240\u4ee5\u4f60\u8981\u600e\u6a23\u505a\u5462\uff1f \u89e3\u6c7a\u65b9\u6cd5 1\uff1a\u6539\u7528\u5c08\u7528\u7684 concurrent map \uff08\u5982 Go \u7684 sync.Map\uff09 \u9019\u500b\u89e3\u6c7a\u65b9\u6cd5\u7c21\u55ae\u76f4\u63a5\uff0c\u6211\u5c31\u4e0d\u518d\u8a73\u7d30\u89e3\u91cb\u4e86\u3002\u7136\u800c\u7531\u65bc\u6211\u5728\u958b\u767c Zoraxy \u7684 high concurrent access \u7279\u8cea\uff0c\u4f7f\u7528 sync.Map \u7684\u8a71\u6703\u505a\u6210\u5f88\u9ad8\u7684 over-head\uff0c\u56e0\u6b64\u6211\u4e26\u4e0d\u662f\u5f88\u60f3\u7528\u5b83\uff08\u4f46\u662f\u4e5f\u4e0d\u5f71\u97ff sync.Map \u4f5c\u70ba\u6c92\u6cd5\u89e3\u6c7a\u6642\u7528\u7684\u6700\u5f8c\u65b9\u6848\u7684\u6c7a\u5b9a\uff09 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104,5],"tags":[],"class_list":["post-1174","post","type-post","status-publish","format-standard","hentry","category-bug-tips","category-development-logs"],"_links":{"self":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1174"}],"version-history":[{"count":2,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1178,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/posts\/1174\/revisions\/1178"}],"wp:attachment":[{"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/imuslab.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}