{"id":236760,"date":"2022-03-10T10:40:31","date_gmt":"2022-03-10T02:40:31","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=236760"},"modified":"2022-02-28T10:42:04","modified_gmt":"2022-02-28T02:42:04","slug":"go-goroutine-linux","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/go-goroutine-linux.html","title":{"rendered":"\u7b80\u5355\u63a2\u8ba8Go\u8bed\u8a00\u7ebf\u7a0b\u5b89\u5168\u4e4b\u4e92\u65a5\u9501\u4e0e\u8bfb\u5199\u9501"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\u4e86Go\u8bed\u8a00\u7ebf\u7a0b\u5b89\u5168\u4e4b\u4e92\u65a5\u9501\u4e0e\u8bfb\u5199\u9501,\u4e92\u65a5\u9501\u662f\u4e3a\u4e86\u5e76\u53d1\u7684\u5b89\u5168,\u5728\u591a\u4e2agoroutine\u5171\u540c\u5de5\u4f5c\u7684\u65f6\u5019,\u5bf9\u4e8e\u5171\u4eab\u7684\u6570\u636e\u5341\u5206\u4e0d\u5b89\u5168,\u800c\u8bfb\u5199\u9501\u6548\u7387\u9769\u547d\uff0c\u4f7f\u7528\u9501\u7684\u65f6\u5019\uff0c\u5b89\u5168\u4e0e\u6548\u7387\u5f80\u5f80\u9700\u8981\u4e92\u76f8\u8f6c\u6362\uff0c\u4e0b\u6587\u8be6\u7ec6\u5185\u5bb9\uff0c\u9700\u8981\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5355\u4e2a\u7ebf\u7a0b\u65f6\u6570\u636e\u64cd\u4f5c\u7684\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b,\u6570\u636e\u7684\u4fee\u6539\u4e5f\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53c2\u4e0e\uff0c\u6570\u636e\u76f8\u5bf9\u6765\u8bf4\u662f\u5b89\u5168\u7684,\u591a\u7ebf\u7a0b\u65f6\u5bf9\u6570\u636e\u64cd\u4f5c\u7684\u4e0d\u6b62\u4e00\u4e2a\u7ebf\u7a0b,\u6240\u4ee5\u540c\u65f6\u5bf9\u6570\u636e\u8fdb\u884c\u4fee\u6539\u7684\u65f6\u5019\u96be\u514d\u7d0a\u4e71<\/p>\n

\u4e00\u3001\u4e92\u65a5\u9501\u662f\u4ec0\u4e48\uff1f<\/strong><\/div>\n
1.\u6982\u5ff5<\/strong><\/span><\/div>\n

\u4e92\u65a5\u9501\u662f\u4e3a\u4e86\u5e76\u53d1\u7684\u5b89\u5168,\u5728\u591a\u4e2agoroutine\u5171\u540c\u5de5\u4f5c\u7684\u65f6\u5019,\u5bf9\u4e8e\u5171\u4eab\u7684\u6570\u636e\u5341\u5206\u4e0d\u5b89\u5168\u5199\u5165\u65f6\u5bb9\u6613\u56e0\u4e3a\u7ade\u4e89\u9020\u6210\u6570\u636e\u4e0d\u5fc5\u8981\u7684\u4e22\u5931\u3002\u4e92\u65a5\u9501\u4e00\u822c\u52a0\u5728\u5171\u4eab\u6570\u636e\u4fee\u6539\u7684\u5730\u65b9\u3002<\/p>\n

2.\u672a\u52a0\u9501<\/strong><\/span><\/div>\n

\u7ebf\u7a0b\u4e0d\u5b89\u5168,\u64cd\u4f5c\u7684\u5168\u5c40\u53d8\u91cf\u4f1a\u8ba1\u7b97\u5f02\u5e38<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"fmt\"\r\n    \"sync\"\r\n)\r\n \r\nvar x int = 0\r\n \r\nvar wg sync.WaitGroup\r\n \r\nfunc add() {\r\n    defer wg.Done()\r\n    for i := 0; i < 5000; i++ {\r\n        x++\r\n    }\r\n}\r\nfunc main() {\r\n    wg.Add(2)\r\n    go add()\r\n    go add()\r\n    wg.Wait()\r\n    fmt.Println(x)\r\n}\r\n\/*\r\n\u6253\u5370\u7ed3\u679c\uff1a(\u6bcf\u6b21\u6253\u5370\u4e0d\u4e00\u6837,\u6b63\u5e38\u7684\u7ed3\u679c\u5e94\u8be5\u662f10000)\r\n    6051\r\n    5059\r\n    5748\r\n    10000\r\n*\/\r\n<\/pre>\n
3.\u52a0\u9501\u4e4b\u540e<\/strong><\/span><\/div>\n

\u7ebf\u7a0b\u5b89\u5168,\u5168\u5c40\u53d8\u91cf\u8ba1\u7b97\u65e0\u5f02\u5e38<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"fmt\"\r\n    \"sync\"\r\n)\r\n \r\nvar x int = 0\r\n \r\nvar wg sync.WaitGroup\r\n \r\n\/\/ \u521b\u5efa\u4e00\u4e2a\u9501\u5bf9\u8c61\r\nvar lock sync.Mutex\r\n \r\nfunc add() {\r\n    defer wg.Done()\r\n    for i := 0; i < 5000; i++ {\r\n        \/\/\u52a0\u9501\r\n        lock.Lock()\r\n        x++\r\n        \/\/\u89e3\u9501\r\n        lock.Unlock()\r\n    }\r\n}\r\nfunc main() {\r\n    wg.Add(2)\r\n    \/\/\u5f00\u542f\u4e24\u4e2a\u7ebf\u7a0b\r\n    go add()\r\n    go add()\r\n    wg.Wait()\r\n    fmt.Println(x)\r\n}\r\n\/*\r\n\u6253\u5370\u7ed3\u679c\uff1a\r\n    \u5168\u4e3a10000\r\n*\/<\/pre>\n
\u4e8c\u3001\u8bfb\u5199\u9501\u3010\u6548\u7387\u9769\u547d\u3011<\/strong><\/div>\n
1.\u4e3a\u4ec0\u4e48\u8bfb\u5199\u9501\u6548\u7387\u9ad8<\/strong><\/span><\/div>\n

\u4f7f\u7528\u9501\u7684\u65f6\u5019\uff0c\u5b89\u5168\u4e0e\u6548\u7387\u5f80\u5f80\u9700\u8981\u4e92\u76f8\u8f6c\u6362\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u7684\u65f6\u5019,\u53ea\u4f1a\u8fdb\u884c\u6570\u636e\u7684\u8bfb\u4e0e\u5199\u3002 \u800c\u8bfb\u4e0e\u8bfb\u4e4b\u95f4\u53ef\u4ee5\u540c\u65f6\u8fdb\u884c\uff0c\u8bfb\u4e0e\u5199\u4e4b\u95f4\u9700\u8981\u4fdd\u8bc1\u5199\u7684\u65f6\u5019\u4e0d\u53bb\u8bfb\u3002\u6b64\u65f6\u4e3a\u4e86\u63d0\u9ad8\u6548\u7387\u5c31\u53d1\u660e\u8bfb\u5199\u9501,\u5728\u8bfb\u5199\u9501\u673a\u5236\u4e0b,\u5b89\u5168\u6ca1\u6709\u4e1d\u6beb\u964d\u4f4e\uff0c\u4f46\u6548\u7387\u8fdb\u884c\u4e86\u6210\u500d\u7684\u63d0\u5347\u63d0\u5347\u7684\u6548\u7387\u5728\u8bfb\u4e0e\u5199\u64cd\u4f5c\u6b21\u6570\u5dee\u5f02\u8d8a\u5927\u65f6\u8d8a\u660e\u663e<\/p>\n

2.\u4f7f\u7528\u65b9\u6cd5<\/strong><\/span><\/div>\n

\u4ee3\u7801\u5982\u4e0b\uff08\u793a\u4f8b\uff09\uff1a<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"fmt\"\r\n    \"sync\"\r\n    \"time\"\r\n)\r\n \r\nvar (\r\n    x      = 0\r\n    rwlock sync.RWMutex\r\n    wg     sync.WaitGroup\r\n)\r\n \r\nfunc write() {\r\n    defer wg.Done()\r\n    rwlock.Lock()\r\n    x++\r\n    rwlock.Unlock()\r\n}\r\n \r\nfunc read() {\r\n    wg.Done()\r\n    \/\/\u5f00\u542f\u8bfb\u9501\r\n    rwlock.RLock()\r\n    fmt.Println(x)\r\n    \/\/\u91ca\u653e\u8bfb\u9501\r\n    rwlock.RUnlock()\r\n}\r\nfunc main() {\r\n    start := time.Now()\r\n    for i := 0; i < 100; i++ {\r\n        wg.Add(1)\r\n        go write()\r\n    }\r\n    \/\/ time.Sleep(time.Second)\r\n    for i := 0; i < 10000; i++ {\r\n        wg.Add(1)\r\n        go read()\r\n    }\r\n    wg.Wait()\r\n    fmt.Println(time.Now().Sub(start))\r\n}\r\n<\/pre>\n
\u4e09\u3001sync.once<\/strong><\/div>\n
1.sync.once\u4ea7\u751f\u80cc\u666f<\/strong><\/span><\/div>\n

\u5728\u591a\u4e2agoroutine\u4e2d\u5f80\u5f80\u4f1a\u7531\u4e8e\u7ebf\u7a0b\u4e0d\u540c\u6b65\u9020\u6210\u6570\u636e\u8bfb\u5199\u7684\u51b2\u7a81,\u7279\u522b\u662f\u5728\u8fdb\u884c\u6587\u4ef6\u6253\u5f00\u5bf9\u8c61\u521b\u5efa\u7684\u65f6\u5019\uff0c\u53ef\u80fd\u4f1a\u9020\u6210\u5411\u5173\u95ed\u7684\u6587\u4ef6\u5199\u5185\u5bb9\uff0c\u4f7f\u7528\u672a\u521d\u59cb\u5316\u7684\u5bf9\u8c61\uff0c\u6216\u8005\u5bf9\u4e00\u4e2a\u5bf9\u8c61\u8fdb\u884c\u591a\u6b21\u521d\u59cb\u5316\u3002<\/p>\n

2.sync.once\u673a\u5236\u6982\u8ff0<\/strong><\/span><\/div>\n

sync.once\u4fdd\u8bc1\u51fd\u6570\u5185\u7684\u4ee3\u7801\u53ea\u6267\u884c\u4e00\u6b21\uff0c \u5b9e\u73b0\u7684\u673a\u5236\u662f\u5728once\u5185\u90e8\u6709\u4e00\u4e2a\u6807\u5fd7\u4f4d,\u5728\u6267\u884c\u4ee3\u7801\u7684\u65f6\u5019\u6267\u884c\u4e00\u6b21\u4e4b\u540e\u6807\u5fd7\u4f4d\u5c06\u7f6e\u4e3a1\u540e\u7eed\u5224\u65ad\u6807\u5fd7\u4f4d,\u5982\u679c\u6807\u5fd7\u4f4d\u88ab\u6539\u4e3a1\u5219\u65e0\u6cd5\u518d\u8fdb\u884c\u64cd\u7eb5<\/p>\n

3.sync.once\u6ce8\u610f\u70b9<\/strong><\/span><\/div>\n

sync.Once.Do()\u4f20\u8fdb\u53bb\u7684\u51fd\u6570\u53c2\u6570\u65e0\u53c2\u65e0\u8fd4\uff0c\u4e00\u4e2aonce\u5bf9\u8c61\u53ea\u80fd\u6267\u884c\u4e00\u6b21Do\u65b9\u6cd5,\u5411Do\u65b9\u6cd5\u5185\u4f20\u591a\u4e2a\u4e0d\u540c\u7684\u51fd\u6570\u65f6\u53ea\u80fd\u6267\u884c\u7b2c\u4e00\u4e2a\u4f20\u8fdb\u53bb\u7684\uff0c\u4f20\u8fdb\u53bbDo\u65b9\u6cd5\u7684\u51fd\u6570\u65e0\u53c2\u65e0\u8fd4,\u53ef\u4ee5\u7528\u51fd\u6570\u95ed\u5305\u628a\u9700\u8981\u7684\u53d8\u91cf\u4f20\u8fdb\u53bb<\/p>\n

4.\u4f7f\u7528\u65b9\u6cd5<\/strong><\/span><\/div>\n

\u4e00\u822c\u7ed3\u5408\u5e76\u53d1\u4f7f\u7528,\u65e8\u5728\u5bf9\u901a\u9053\u6216\u6587\u4ef6\u53ea\u8fdb\u884c\u4e00\u6b21\u5173\u95ed<\/p>\n

\r\nfunc f2(a <-chan int, b chan<- int) {\r\n    for {\r\n        x, ok := <-a\r\n        if !ok {\r\n            break\r\n        }\r\n        fmt.Println(x)\r\n        b <- x * 10\r\n    }\r\n    \/\/ \u786e\u4fddb\u901a\u9053\u53ea\u5173\u95ed\u4e00\u6b21\r\n    once.Do(func() {\r\n        close(b)\r\n    })\r\n}<\/pre>\n
\u56db\u3001atomic\u539f\u5b50\u5305\u64cd\u4f5c<\/strong><\/div>\n

\u539f\u5b50\u5305\u5c06\u6307\u5b9a\u7684\u6570\u636e\u8fdb\u884c\u5b89\u5168\u7684\u52a0\u51cf\u4ea4\u6362\u64cd\u4f5c\uff1b \u7f51\u4e0a\u8fd8\u6709\u4e00\u5927\u5806\u5173\u4e8e\u539f\u5b50\u5305\u7684api\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u53ef\u4ee5\u81ea\u884c\u767e\u5ea6,\u8fd9\u91cc\u5c31\u4e0d\u7ec6\u7ec6\u9610\u8ff0\u4e86<\/p>\n

\r\npackage main\r\n \r\nimport (\r\n    \"fmt\"\r\n    \"sync\"\r\n    \"sync\/atomic\"\r\n)\r\n \r\nvar x int64 = 0\r\n \r\nvar wg sync.WaitGroup\r\n \r\n\/*\r\n    \u539f\u5b50\u64cd\u4f5c\u662f\u5c06\u6570\u636e\u8fdb\u884c\u6253\u5305\u67b7\u9501,\u76f4\u63a5\u901a\u8fc7\u6307\u5b9a\u7684\u51fd\u6570\u8fdb\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\r\n    \u53ef\u4ee5\u4f7f\u7528load\u8bfb\u53d6\u3001store\u5199\u5165\u3001add\u4fee\u6539\u3001swap\u4ea4\u6362\u3002\r\n    \/\/ \u7c7b\u4f3c\u4e8e\u8bfb\u53d6\u4e00\u4e2a\u53d8\u91cf\u3001\u5bf9\u4e00\u4e2a\u53d8\u91cf\u8fdb\u884c\u8d4b\u503c\r\n*\/\r\nfunc addone() {\r\n    \/\/ \u6ca1\u6709\u52a0\u9501\u8fdb\u884c\u5e76\u53d1\u7684\u8bdd\uff0c\u4f1a\u4ea7\u751f\u6570\u636e\u4e22\u5931\u7684\u60c5\u51b5\r\n    defer wg.Done()\r\n    \/\/ x++\r\n \r\n    \/\/ \u4e0d\u7528\u52a0\u9501\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7684\u884c\u4e91\u6d41\u6c34\r\n    \/\/ \u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8fdb\u884c\u64cd\u4f5c\u7684\u6570\u636e,\u7b2c\u4e8c\u4e2a\u662f\u589e\u52a0\u7684\u6b65\u957f\r\n    atomic.AddInt64(&x, 1)\r\n \r\n}\r\nfunc csf() {\r\n    \/\/ \u8fdb\u884c\u6bd4\u8f83\u76f8\u7b49\u5219\u5c06\u65b0\u503c\u66ff\u6362\u65e7\u503c\r\n    ok := atomic.CompareAndSwapInt64(&x, 100, 200)\r\n    fmt.Println(ok, x)\r\n}\r\n \r\nfunc main() {\r\n    for i := 0; i < 50000; i++ {\r\n        wg.Add(1)\r\n        go addone()\r\n    }\r\n    wg.Wait()\r\n    fmt.Println(x)\r\n    x = 100\r\n    csf()\r\n    fmt.Println(123)\r\n}<\/pre>\n
\u603b\u7ed3\uff1a<\/strong><\/div>\n

\u8bfb\u5199\u9501\u533a\u5206\u8bfb\u8005\u548c\u5199\u8005\uff0c\u800c\u4e92\u65a5\u9501\u4e0d\u533a\u5206 \u4e92\u65a5\u9501\u540c\u4e00\u65f6\u95f4\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u8bbf\u95ee\u8be5\u5bf9\u8c61\uff0c\u65e0\u8bba\u8bfb\u5199\uff1b\u8bfb\u5199\u9501\u540c\u4e00\u65f6\u95f4\u5185\u53ea\u5141\u8bb8\u4e00\u4e2a\u5199\u8005\uff0c \u4f46\u662f\u5141\u8bb8\u591a\u4e2a\u8bfb\u8005\u540c\u65f6\u8bfb\u5bf9\u8c61\u3002 \u8054\u7cfb\uff1a\u8bfb\u5199\u9501\u5728\u83b7\u53d6\u5199\u9501\u7684\u65f6\u5019\u673a\u5236\u7c7b\u4f3c\u4e8e\u4e92\u65a5\u9501\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5355\u4e2a\u7ebf\u7a0b\u65f6\u6570\u636e\u64cd\u4f5c\u7684\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b,\u6570\u636e\u7684\u4fee\u6539\u4e5f\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53c2\u4e0e\uff0c\u6570\u636e\u76f8\u5bf9\u6765\u8bf4\u662f\u5b89\u5168\u7684,\u591a\u7ebf\u7a0b\u65f6\u5bf9\u6570\u636e\u64cd\u4f5c\u7684\u4e0d\u6b62 […]<\/p>\n","protected":false},"author":362,"featured_media":220304,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-236760","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=236760"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236760\/revisions"}],"predecessor-version":[{"id":236943,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/236760\/revisions\/236943"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/220304"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=236760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=236760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=236760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}