{"id":208163,"date":"2025-07-08T12:19:20","date_gmt":"2025-07-08T04:19:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208163\/"},"modified":"2025-07-08T12:19:20","modified_gmt":"2025-07-08T04:19:20","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e6%89%80%e6%9c%89%e7%9a%84-goroutine-%e9%83%bd%e5%9c%a8%e7%9d%a1%e8%a7%89-%e4%b8%8e%e8%82%a1%e7%a5%a8%e4%bb%a3%e7%a0%81%e5%8f%91%e7%94%9f%e6%ad%bb%e9%94%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208163\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u6240\u6709\u7684 goroutine \u90fd\u5728\u7761\u89c9\u2014\u2014\u4e0e\u80a1\u7968\u4ee3\u7801\u53d1\u751f\u6b7b\u9501\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48\u6240\u6709\u7684 goroutine \u90fd\u5728\u7761\u89c9\u2014\u2014\u4e0e\u80a1\u7968\u4ee3\u7801\u53d1\u751f\u6b7b\u9501\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-29 18:36:30<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4e3a\u4ec0\u4e48\u6240\u6709\u7684 goroutine \u90fd\u5728\u7761\u89c9\u2014\u2014\u4e0e\u80a1\u7968\u4ee3\u7801\u53d1\u751f\u6b7b\u9501\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5df2\u7ecf\u4f7f\u7528\u4e86\u4e00\u6bb5\u65f6\u95f4\uff0c\u4f46\u4ecd\u7136\u65e0\u6cd5\u5f04\u6e05\u695a\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u50f5\u5c40\uff08https:\/\/play.golang.org\/p\/ineul_ktmja\uff09\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"time\"\n)\n\nfunc main() {\n    ctx, cancel := context.withtimeout(context.background(), 3*time.second)\n\n    t := time.newticker(time.second)\n    for range t.c {\n        select {\n        case &lt;-ctx.done():\n            t.stop()\n        default:\n            print(\".\")\n        }\n    }\n\n    cancel()\n}<\/pre>\n<p>\u6211\u5e0c\u671b\u5173\u95ed\u7684\u4e0a\u4e0b\u6587\u80fd\u591f\u5173\u95ed\u80a1\u7968\u901a\u9053\uff0c\u8be5\u901a\u9053\u5e94\u8be5\u9000\u51fa\u8303\u56f4\u5faa\u73af\uff0c\u4ece\u800c\u6e05\u7406\u4e0a\u4e0b\u6587\u3002\u76f8\u53cd\uff1a<\/p>\n<pre>...fatal error: all goroutines are asleep - deadlock!\n\ngoroutine 1 [chan receive]:\nmain.main()\n        \/Users\/andig\/htdocs\/test.go:12 +0xaf\nexit status 2<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5b83\u662f\u5728 Stop() \u7684\u6587\u6863\u4e2d\u7f16\u5199\u7684\uff1a<\/p>\n<p>Stop \u4e0d\u4f1a\u5173\u95ed\u901a\u9053\uff0c\u4ee5\u9632\u6b62\u4ece\u901a\u9053\u8bfb\u53d6\u7684\u5e76\u53d1 Goroutine \u770b\u5230\u9519\u8bef\u7684\u201ctick\u201d\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u4e3a\u4ec0\u4e48\u6240\u6709\u7684 goroutine \u90fd\u5728\u7761\u89c9\u2014\u2014\u4e0e\u80a1\u7968\u4ee3\u7801\u53d1\u751f\u6b7b\u9501\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208163","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208163"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208163\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}