{"id":43281,"date":"2024-12-01T14:01:28","date_gmt":"2024-12-01T06:01:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/43281\/"},"modified":"2024-12-01T14:01:28","modified_gmt":"2024-12-01T06:01:28","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-select-%e4%b8%8e-select-case-%e4%bc%9a%e5%af%bc%e8%87%b4%e4%b8%8d%e5%90%8c%e7%9a%84%e8%b0%83%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/43281\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6"},"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 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6<\/span><\/p>\n<h1>\u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 17:57:38<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<h2>1.\u9009\u62e9{\u6848\u4f8b}<\/h2>\n<pre>\"fmt\"\n    \"runtime\"\n    \"time\"\n)\n\nfunc main() {\n    runtime.gomaxprocs(1)\n\n    go func() {\n        fmt.println(\"a\")\n    }()\n    go func() {\n        fmt.println(\"b\")\n    }()\n    go func() {\n        fmt.println(\"c\")\n    }()\n\n    fmt.println(\"d\")\n\n    select {\n    case _ = &lt;-time.after(time.second):\n    }\n}<\/pre>\n<p><code>result:dabc<\/code><\/p>\n<h2>2.\u9009\u62e9{}<\/h2>\n<pre>package main\nimport (\n    \"fmt\"\n    \"runtime\"\n\n)\nfunc main() {\n    runtime.GOMAXPROCS(1)\n\n    go func() {\n        fmt.Printf(\"a\")\n\n    }()\n\n    go func() {\n        fmt.Printf(\"b\")\n\n    }()\n\n    go func() {\n        fmt.Printf(\"c\")\n\n    }()\n\n    fmt.Printf(\"d\")\n    select {}\n\n}<\/pre>\n<p><code>result:dcab<\/code><\/p>\n<p>\u56e0\u4e3a\u6211\u8bbe\u7f6e\u4e86\u201cruntime.gomaxprocs(1)\u201d\uff0c\u6240\u4ee5\u6211\u7684\u7a0b\u5e8f\u4e2d\u53ea\u6709\u4e00\u4e2a\u201c\u5904\u7406\u5668\u201d\u3002<\/p>\n<p>\u4e00\u4e2a\u201cprocessor\u201d\u53ea\u6709\u4e00\u4e2a\u201crunq\u201d\uff0cin\/out\u64cd\u4f5c\u662f\u201cfifo\u201d<\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\u201c\u4e3a\u4ec0\u4e48\u603b\u662f&#8230;&#8230;\u800c\u4e0d\u662f\u5170\u5fb7&#8230;&#8230;\u201d<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>Finally figured it out.<\/p>\n<p>Processor.runq is FIFO, there&#8217;s another variable(runnext) here, runnext saves the lastest goroutine which where will be processed next.<\/p>\n<p>The latest goroutine kicks <em>p<\/em>.runnext <\/p>\n<p>Timer creates a goroutine ,and this goroutine kicks <em>p<\/em>.runnext.<\/p>\n<p>Processor\u7684runnext \u88ab\u5b9a\u65f6\u5668\u521b\u5efa\u7684goroutine kick\u8c03, \u5bfc\u81f4\u4e0b\u4e00\u4e2a\u6267\u884c\u7684\u662f\u5b9a\u65f6\u5668\u7684goroutine,\u7136\u540e\u5176\u4ed6\u7684FIFO<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6 \u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6 \u6765\u6e90\uff1astackoverflow 2024-05-01 17:57:38 0\u6d4f\u89c8 \u6536\u85cf \u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728Golang\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u4e3a\u4ec0\u4e48 select{ } \u4e0e select {case} \u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u8c03\u5ea6\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u95ee\u9898\u5185\u5bb9 1.\u9009\u62e9{\u6848\u4f8b} &#8220;fmt&#8221; &#8220;runtime&#8221; &#8220;time&#8221; ) func main() { runtime.gomaxprocs(1) go func() { fmt.println(&#8220;a&#8221;) }() go func() { fmt.println(&#8220;b&#8221;) }() go func() { fmt.println(&#8220;c&#8221;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-43281","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=43281"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/43281\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=43281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=43281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=43281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}