Go \u7684 slice \u5728 \u5e76\u540c\u8bfb\u53d6\u65f6\u662f\u5b89\u5168\u7684\uff0c\u4f46\u5e76\u540c\u5199\u5165\u53ef\u80fd\u5bfc\u81f4\u7ade\u751f\u6761\u4ef6\u3002\u672c\u6587\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u4e92\u63a7\u9501\uff08mutex\uff09\u4fdd\u62a4 slice\u7684\u5e76\u540c\u8bbf\u95ee\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002\n\nGo \u7684 slice \u662f\u5e76\u540c\u5b89\u5168\u7684\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a goroutine \u4e2d\u5e76\u540c\u8bfb\u53d6\u548c\u5199\u5165 slice\u3002\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5f53\u591a\u4e2a goroutine \u540c\u65f6\u5bf9\u540c\u4e00\u4e2a slice \u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u53d1\u751f\u7ade\u751f\u6761\u4ef6\uff08race condition\uff09\u5bfc\u81f4\u4e0d\u786e\u5b9a\u7684\u7ed3\u679c\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e92\u63a7\u9501\uff08mutex\uff09\u6765\u4fdd\u62a4 slice\u7684\u5e76\u540c\u8bbf\u95ee\u3002\n\n\u5728 Go \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 sync \u5305\u4e2d\u7684 Mutex \u7c7b\u578b\u6765\u5b9e\u73b0\u4e92\u63a7\u9501\u3002\u901a\u8fc7\u5728\u5bf9 slice \u8fdb\u884c\u8bfb\u53d6\u5199\u5165\u64cd\u4f5c\u4e4b\u524d\u5148\u8c03\u7528 Lock() \u65b9\u6cd5\u83b7\u5f97\u9501\uff0c\u5e76\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u8c03\u7528 Unlock() \u65b9\u6cd5\u89e3\u653e\u9501\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u540c\u65f6\u53ea\u6709\u4e00\u4e2a goroutine \u80fd\u591f\u5bf9 slice \u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u3002\n\n\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4e92\u63a7\u9501\u4fdd\u62a4 slice\u5e76\u540c\u8bbf\u95ee\u7684\u793a\u4f8b\u4ee3\u7801\uff1a\n\ngo\npackage main\n\nimport (\n\t"fmt"\n\t"sync"\n)\n\nfunc main() {\n\tvar mu sync.Mutex\n\tvar wg sync.WaitGroup\n\nslice := []int{}\n\nfor i := 0; i < 10; i++ {\n\twg.Add(1)\n\tgo func(index int) {\n\t\tdefer wg.Done()\n\n\t\tmu.Lock()\n\t\tdefer mu.Unlock()\n\n\t\tslice = append(slice, index)\n\t}(i)\n}\n\n\twg.Wait()\n\tfmt.Println(slice)\n}\n\n\n\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7 sync.Mutex \u6765\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e92\u63a7\u9501 mu\u3002\u5728\u6bcf\u4e2a goroutine \u4e2d\uff0c\u5728\u5bf9 slice \u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u524d\u8c03\u7528 mu.Lock() \u83b7\u5f97\u9501\uff0c\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u8c03\u7528 mu.Unlock() \u89e3\u653e\u9501\u3002\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u540c\u65f6\u53ea\u6709\u4e00\u4e2a goroutine \u80fd\u591f\u5bf9 slice \u8fdb\u884c\u5199\u5165\u64cd\u4f5c\uff0c\u5b8c\u5168\u907f\u514d\u4e86\u7ade\u751f\u6761\u4ef6\u3002\n\n\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u867d\u7136 slice \u672c\u8eab\u662f\u5e76\u540c\u5b89\u5168\u7684\uff0c\u4f46\u662f\u5bf9 slice \u4e2d\u5143\u7d20\u7684\u4fee\u6539\u4ecd\u7136\u9700\u8981\u4fdd\u8bc1\u5e76\u540c\u5b89\u5168\u3002\u5982\u679c\u591a\u4e2a goroutine \u540c\u65f6\u5bf9\u540c\u4e00\u4e2a slice \u4e2d\u7684\u540c\u4e00\u4e2a\u5143\u7d20\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\uff0c\u4ecd\u7136\u53ef\u80fd\u5bfc\u81f4\u7ade\u751f\u6761\u4ef6\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e92\u63a7\u9501\u6216\u8005\u5176\u5b83\u5e76\u540c\u539f\u7406\u6765\u4fdd\u8bc1\u5bf9\u5143\u7d20\u7684\u5e76\u540c\u8bbf\u95ee\u7684\u5b89\u5168\u6027\u3002

Go Slice 并发安全:如何避免竞态条件

原文地址: https://www.cveoy.top/t/topic/qCmA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录