{"id":445,"date":"2011-02-18T13:18:17","date_gmt":"2011-02-18T05:18:17","guid":{"rendered":"http:\/\/www.hawkwithwind.net\/blog\/?p=445"},"modified":"2011-02-18T13:18:27","modified_gmt":"2011-02-18T05:18:27","slug":"%e5%8d%8f%e7%a8%8b%e7%9a%84c%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.hawkwithwind.net\/blog\/2011\/02\/18\/%e5%8d%8f%e7%a8%8b%e7%9a%84c%e5%ae%9e%e7%8e%b0\/","title":{"rendered":"\u534f\u7a0b\u7684C\u5b9e\u73b0"},"content":{"rendered":"<p>\u4eca\u5929\u6b63\u597d\u8ddfownwaterloo\u804a\u5230\u534f\u7a0b\uff0c\u4e8e\u662f\u67e5\u4e86\u67e5\u8d44\u6599\uff0c\u987a\u4fbf\u5199\u4e2a\u535a\u5ba2\u8bb0\u5f55\u4e00\u4e0b\u5427\u3002<\/p>\n<p>\u6211\u4e3b\u8981\u53c2\u8003\u7684\u662f\u8fd9\u7bc7\u8d44\u6599<a href=\"http:\/\/www.chiark.greenend.org.uk\/~sgtatham\/coroutines.html\">http:\/\/www.chiark.greenend.org.uk\/~sgtatham\/coroutines.html<\/a>\uff0c\u662fSimon Tatham\u63d0\u51fa\u7684\u4e00\u4e2a\u534f\u7a0b\u7684C\u5b9e\u73b0\uff0c\u975e\u5e38\u6709\u610f\u601d\u3002<\/p>\n<p>\u534f\u7a0b\u7684\u601d\u60f3\u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u4e2a\u4efb\u52a1\u5982\u4f55\u5206\u4eabCPU\u8fd9\u4e2a\u95ee\u9898\u7684\u3002\u7ebf\u7a0b\u5728\u5f88\u591a\u534f\u4f5c\u95ee\u9898\u4e0a\u5904\u7406\u7684\u4e0d\u597d\uff0c\u800c\u4e14\u9700\u8981\u9501\u673a\u5236\uff0c\u5bfc\u81f4\u8fd0\u884c\u7f13\u6162\uff0c\u4e0d\u6613\u7406\u89e3\uff0c\u5bb9\u6613\u51fa\u9519\u3002\u534f\u7a0b\u7684\u601d\u60f3\u662f\uff0c\u4e00\u7cfb\u5217\u4e92\u76f8\u4f9d\u8d56\u7684\u534f\u7a0b\u95f4\u4f9d\u6b21\u4f7f\u7528CPU\uff0c\u6bcf\u6b21\u53ea\u6709\u4e00\u4e2a\u534f\u7a0b\u5de5\u4f5c\uff0c\u800c\u5176\u4ed6\u534f\u7a0b\u5904\u4e8e\u4f11\u7720\u72b6\u6001\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0c\u534f\u7a0b\u662f\u81ea\u5df1\u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u5e76\u4ea4\u4ed8\u4ed6\u671f\u671b\u7684\u4e0b\u4e00\u4e2a\u534f\u7a0b\u8fd0\u884c\uff0c\u800c\u4e0d\u662f\u5728\u4efb\u4f55\u65f6\u5019\u90fd\u6709\u53ef\u80fd\u88ab\u7cfb\u7edf\u8c03\u5ea6\u6253\u65ad\u3002\u56e0\u6b64\u534f\u7a0b\u7684\u4f7f\u7528\u66f4\u52a0\u6e05\u6670\u6613\u61c2\uff0c\u5e76\u4e14\u591a\u6570\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u9501\u673a\u5236\uff08\u6216\u8005\u8bf4\u4ed6\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5168\u5c40\u9501\uff09\u3002<\/p>\n<p>Simon\u7684\u4e3e\u4f8b\u662f\u4e00\u4e2a\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u4f8b\u5b50\u3002\u4f20\u7edf\u7684\u7a0b\u5e8f\u53ef\u80fd\u662f\u751f\u4ea7\u8005\u4e00\u4e2a\u5faa\u73af\u4e0d\u65ad\u4ea7\u751f\u5b57\u7b26\uff0c\u4e4b\u540e\u9000\u51fa\u3002\u800c\u6d88\u8d39\u8005\u4e00\u4e2a\u5faa\u73af\u4e0d\u65ad\u8bfb\u53d6\u5b57\u7b26\uff0c\u5e76\u5904\u7406\u4e4b\u3002\u4f7f\u7528\u65f6\u6216\u8bb8\u4f1a\u7528\u4e00\u4e2a\u7ba1\u9053\uff0c\u5c06\u751f\u4ea7\u8005\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6d88\u8d39\u8005\u7684\u8f93\u5165\u3002\u4ece\u800c\u4f7f\u4e24\u8005\u534f\u4f5c\u3002<\/p>\n<p>Simon\u63d0\u51fa\uff0c\u5982\u4f55\u4f7f\u8fd9\u6837\u7684\u7b80\u5355\u4efb\u52a1\u65e0\u9700\u901a\u8fc7\u7ba1\u9053\u8fd9\u7c7b\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u91cd\u578b\u673a\u5236\uff0c\u5c31\u80fd\u5b8c\u6210\u3002\u4ed6\u63d0\u51fa\uff0c\u751f\u4ea7\u8005\u6216\u8005\u6d88\u8d39\u8005\u4e4b\u95f4\uff0c\u6709\u4e00\u65b9\u9700\u8981\u6539\u53d8\u81ea\u5df1\u7684\u5de5\u4f5c\u6a21\u5f0f\u3002\u4e0d\u518d\u662f\u5728\u5faa\u73af\u4e2d\u4e0d\u65ad\u5904\u7406\u4efb\u52a1\uff0c\u800c\u662f\u4e00\u6b21\u53ea\u5904\u7406\u4e00\u4e2a\u5b57\u7b26\u7136\u540e\u7acb\u523b\u8fd4\u56de\u3002\u800c\u53e6\u4e00\u65b9\u4f9d\u65e7\u4fdd\u6301\u539f\u72b6\uff0c\u53ea\u9700\u5728\u539f\u672c\u8f93\u51fa\u5230\u6216\u8bfb\u53d6\u81ea\u6807\u51c6IO\u7684\u5730\u65b9\u4fee\u6539\u4e3a\u8c03\u7528\u5bf9\u65b9\u7684\u90a3\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u4f46\u8fd9\u79cd\u5199\u6cd5\u96be\u4ee5\u7ef4\u62a4\u3002\u4ed6\u63d0\u51fa\u4e86\u534f\u7a0b\u7684\u6982\u5ff5\uff0c\u5e76\u671f\u5f85\u5199\u51fa\u7c7b\u4f3c\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>int function(void) {\r\n    int i;\r\n    for (i = 0; i &lt; 10; i++)\r\n        return i;   \/* won't work, but wouldn't it be nice *\/\r\n}<\/code><\/pre>\n<p>\u5728\u8fd9\u91ccreturn\u8bed\u53e5\u5e76\u4e0d\u662f\u7ec8\u6b62\u51fd\u6570\uff0c\u800c\u662f\u5c06\u51fd\u6570\u8f6c\u5165\u7761\u7720\u7684\u72b6\u6001\uff0c\u5e76\u5c06CPU\u4ea4\u4ed8\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u3002\u4f8b\u5982\u751f\u4ea7\u8005\u5199\u5165\u4e00\u4e2a\u5b57\u7b26\u540e\uff0c\u8c03\u7528\u7c7b\u4f3c\u7684return\u8bed\u53e5\uff0c\u4ea4\u4ed8\u6d88\u8d39\u8005\u5904\u7406\u8fd9\u4e2a\u5b57\u7b26\u3002\u7a0d\u540e\u5f53\u6d88\u8d39\u8005\u5904\u7406\u5b8c\u5e76\u8c03\u7528return\u8bed\u53e5\u540e\uff0c\u80fd\u91cd\u65b0\u6fc0\u6d3b\u751f\u4ea7\u8005\u534f\u7a0b\uff0c\u5e76\u7ee7\u7eed\u8fd9\u4e2afor\u5faa\u73af\u3002<\/p>\n<p>\u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\u800c\u4e0d\u9700\u4f7f\u7528\u6c47\u7f16\u4ee3\u7801\u53bbhack\u5806\u6808\u548c\u5bc4\u5b58\u5668\u5462\uff1f\u4ed6\u7ed9\u51fa\u7684\u6700\u521d\u7684\u5b9e\u73b0\u662f\u4f7f\u7528goto\u8bed\u53e5\u3002<\/p>\n<pre><code>int function(void) {\r\n    static int i, state = 0;\r\n    switch (state) {\r\n        case 0: goto LABEL0;\r\n        case 1: goto LABEL1;\r\n    }\r\n    LABEL0: \/* start of function *\/\r\n    for (i = 0; i &lt; 10; i++) {\r\n        state = 1; \/* so we will come back to LABEL1 *\/\r\n        return i;\r\n        LABEL1:; \/* resume control straight after the return *\/\r\n    }\r\n}<\/code><\/pre>\n<p>\u5de7\u5999\u7684\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u5b58\u50a8\u51fd\u6570\u72b6\u6001\uff0c\u5e76\u4f7f\u7528goto\u8bed\u53e5\u6765\u7ee7\u7eedfor\u5faa\u73af\u3002\u4f46\u8fd9\u79cd\u5199\u6cd5\u4e0d\u591f\u4f18\u7f8e\uff0c\u4e8e\u662f\u53c8\u5f15\u5165\u4e86Duff&#8217;s device\u3002<\/p>\n<pre><code>switch (count % 8) {\r\n        case 0:        do {  *to = *from++;\r\n        case 7:              *to = *from++;\r\n        case 6:              *to = *from++;\r\n        case 5:              *to = *from++;\r\n        case 4:              *to = *from++;\r\n        case 3:              *to = *from++;\r\n        case 2:              *to = *from++;\r\n        case 1:              *to = *from++;\r\n                       } while ((count -= 8) &gt; 0);\r\n    }<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u79cdtrick\u6765\u5c06switch-case\u8bed\u53e5\u4f5c\u4e3a\u5faa\u73af\u4e2d\u7684\u8df3\u8f6c\u3002\u8fd9\u5c31\u907f\u514d\u4e86goto-label\u8bed\u53e5\u9700\u8981\u540c\u65f6\u7ef4\u62a4goto\u548clabel\u4e24\u5904\u4ee3\u7801\u7684\u9ebb\u70e6\u3002\u4e8e\u662f\u524d\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u53d8\u6210\u8fd9\u4e2a\u6837\u5b50\u3002<\/p>\n<pre><code>int function(void) {\r\n    static int i, state = 0;\r\n    switch (state) {\r\n        case 0: \/* start of function *\/\r\n        for (i = 0; i &lt; 10; i++) {\r\n            state = 1; \/* so we will come back to \"case 1\" *\/\r\n            return i;\r\n            case 1:; \/* resume control straight after the return *\/\r\n        }\r\n    }\r\n}<\/code><\/pre>\n<p>\u4ed6\u7684\u6587\u7ae0\u540e\u9762\u8fd8\u6709\u4e00\u4e9b\u5185\u5bb9\uff0c\u4f8b\u5982\u5982\u4f55\u4f7f\u7528\u5b8f\u5305\u88c5\u8fd9\u5957\u673a\u5236\u3002\u5982\u4f55\u4f7f\u7528__LINE__\u5b8f\u907f\u514dstate\u88ab\u8d4b\u4e88\u76f8\u540c\u7684\u6570\u503c\u3002\u5982\u4f55\u907f\u514d\u591a\u7ebf\u7a0b\u8c03\u7528\u4e0b\u5e72\u6270\u9759\u6001\u53d8\u91cf\u7b49\u7b49\u3002\u6211\u8fd9\u91cc\u5c31\u4e0d\u8d58\u8ff0\u4e86\uff0c\u5927\u5bb6\u6709\u5174\u8da3\u53ef\u53c2\u8003\u539f\u6587\u3002<br \/>\n\u603b\u4e4b\u8bfb\u6b64\u6587\u7684\u4e24\u4e2a\u6536\u83b7\u4e00\u4e2a\u662f\u8ba4\u8bc6\u534f\u7a0b\uff0c\u4e00\u4e2a\u662f\u5b66\u4e60\u5230\u4e86\u4e00\u79cd\u8be1\u8c32\u7684C\u8bed\u8a00\u7528\u6cd5\u3002\u975e\u5e38\u5f00\u5fc3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u6b63\u597d\u8ddfownwaterloo\u804a\u5230\u534f\u7a0b\uff0c\u4e8e\u662f\u67e5\u4e86\u67e5\u8d44\u6599\uff0c\u987a\u4fbf\u5199\u4e2a\u535a\u5ba2\u8bb0\u5f55\u4e00\u4e0b\u5427\u3002 \u6211\u4e3b\u8981\u53c2\u8003\u7684\u662f\u8fd9\u7bc7\u8d44\u6599http:\/\/www.chiark.greenend.org.uk\/~sgtatham\/coroutines.html\uff0c\u662fSimon Tatham\u63d0\u51fa\u7684\u4e00\u4e2a\u534f\u7a0b\u7684C\u5b9e\u73b0\uff0c\u975e\u5e38\u6709\u610f\u601d\u3002 \u534f\u7a0b\u7684\u601d\u60f3\u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u4e2a\u4efb\u52a1\u5982\u4f55\u5206\u4eabCPU\u8fd9\u4e2a\u95ee\u9898\u7684\u3002\u7ebf\u7a0b\u5728\u5f88\u591a\u534f\u4f5c\u95ee\u9898\u4e0a\u5904\u7406\u7684\u4e0d\u597d\uff0c\u800c\u4e14\u9700\u8981\u9501\u673a\u5236\uff0c\u5bfc\u81f4\u8fd0\u884c\u7f13\u6162\uff0c\u4e0d\u6613\u7406\u89e3\uff0c\u5bb9\u6613\u51fa\u9519\u3002\u534f\u7a0b\u7684\u601d\u60f3\u662f\uff0c\u4e00\u7cfb\u5217\u4e92\u76f8\u4f9d\u8d56\u7684\u534f\u7a0b\u95f4\u4f9d\u6b21\u4f7f\u7528CPU\uff0c\u6bcf\u6b21\u53ea\u6709\u4e00\u4e2a\u534f\u7a0b\u5de5\u4f5c\uff0c\u800c\u5176\u4ed6\u534f\u7a0b\u5904\u4e8e\u4f11\u7720\u72b6\u6001\u3002\u4e0e\u7ebf\u7a0b\u4e0d\u540c\uff0c\u534f\u7a0b\u662f\u81ea\u5df1\u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u5e76\u4ea4\u4ed8\u4ed6\u671f\u671b\u7684\u4e0b\u4e00\u4e2a\u534f\u7a0b\u8fd0\u884c\uff0c\u800c\u4e0d\u662f\u5728\u4efb\u4f55\u65f6\u5019\u90fd\u6709\u53ef\u80fd\u88ab\u7cfb\u7edf\u8c03\u5ea6\u6253\u65ad\u3002\u56e0\u6b64\u534f\u7a0b\u7684\u4f7f\u7528\u66f4\u52a0\u6e05\u6670\u6613\u61c2\uff0c\u5e76\u4e14\u591a\u6570\u60c5\u51b5\u4e0b\u4e0d\u9700\u8981\u9501\u673a\u5236\uff08\u6216\u8005\u8bf4\u4ed6\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u5168\u5c40\u9501\uff09\u3002 Simon\u7684\u4e3e\u4f8b\u662f\u4e00\u4e2a\u751f\u4ea7\u8005\u6d88\u8d39\u8005\u4f8b\u5b50\u3002\u4f20\u7edf\u7684\u7a0b\u5e8f\u53ef\u80fd\u662f\u751f\u4ea7\u8005\u4e00\u4e2a\u5faa\u73af\u4e0d\u65ad\u4ea7\u751f\u5b57\u7b26\uff0c\u4e4b\u540e\u9000\u51fa\u3002\u800c\u6d88\u8d39\u8005\u4e00\u4e2a\u5faa\u73af\u4e0d\u65ad\u8bfb\u53d6\u5b57\u7b26\uff0c\u5e76\u5904\u7406\u4e4b\u3002\u4f7f\u7528\u65f6\u6216\u8bb8\u4f1a\u7528\u4e00\u4e2a\u7ba1\u9053\uff0c\u5c06\u751f\u4ea7\u8005\u7684\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6d88\u8d39\u8005\u7684\u8f93\u5165\u3002\u4ece\u800c\u4f7f\u4e24\u8005\u534f\u4f5c\u3002 Simon\u63d0\u51fa\uff0c\u5982\u4f55\u4f7f\u8fd9\u6837\u7684\u7b80\u5355\u4efb\u52a1\u65e0\u9700\u901a\u8fc7\u7ba1\u9053\u8fd9\u7c7b\u64cd\u4f5c\u7cfb\u7edf\u76f8\u5173\u7684\u91cd\u578b\u673a\u5236\uff0c\u5c31\u80fd\u5b8c\u6210\u3002\u4ed6\u63d0\u51fa\uff0c\u751f\u4ea7\u8005\u6216\u8005\u6d88\u8d39\u8005\u4e4b\u95f4\uff0c\u6709\u4e00\u65b9\u9700\u8981\u6539\u53d8\u81ea\u5df1\u7684\u5de5\u4f5c\u6a21\u5f0f\u3002\u4e0d\u518d\u662f\u5728\u5faa\u73af\u4e2d\u4e0d\u65ad\u5904\u7406\u4efb\u52a1\uff0c\u800c\u662f\u4e00\u6b21\u53ea\u5904\u7406\u4e00\u4e2a\u5b57\u7b26\u7136\u540e\u7acb\u523b\u8fd4\u56de\u3002\u800c\u53e6\u4e00\u65b9\u4f9d\u65e7\u4fdd\u6301\u539f\u72b6\uff0c\u53ea\u9700\u5728\u539f\u672c\u8f93\u51fa\u5230\u6216\u8bfb\u53d6\u81ea\u6807\u51c6IO\u7684\u5730\u65b9\u4fee\u6539\u4e3a\u8c03\u7528\u5bf9\u65b9\u7684\u90a3\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\u3002 \u4f46\u8fd9\u79cd\u5199\u6cd5\u96be\u4ee5\u7ef4\u62a4\u3002\u4ed6\u63d0\u51fa\u4e86\u534f\u7a0b\u7684\u6982\u5ff5\uff0c\u5e76\u671f\u5f85\u5199\u51fa\u7c7b\u4f3c\u8fd9\u6837\u7684\u4ee3\u7801\uff1a int function(void) { int i; for (i = 0; i &lt; 10; i++) return i; \/* won&#8217;t work, but wouldn&#8217;t it be nice *\/ } \u5728\u8fd9\u91ccreturn\u8bed\u53e5\u5e76\u4e0d\u662f\u7ec8\u6b62\u51fd\u6570\uff0c\u800c\u662f\u5c06\u51fd\u6570\u8f6c\u5165\u7761\u7720\u7684\u72b6\u6001\uff0c\u5e76\u5c06CPU\u4ea4\u4ed8\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u3002\u4f8b\u5982\u751f\u4ea7\u8005\u5199\u5165\u4e00\u4e2a\u5b57\u7b26\u540e\uff0c\u8c03\u7528\u7c7b\u4f3c\u7684return\u8bed\u53e5\uff0c\u4ea4\u4ed8\u6d88\u8d39\u8005\u5904\u7406\u8fd9\u4e2a\u5b57\u7b26\u3002\u7a0d\u540e\u5f53\u6d88\u8d39\u8005\u5904\u7406\u5b8c\u5e76\u8c03\u7528return\u8bed\u53e5\u540e\uff0c\u80fd\u91cd\u65b0\u6fc0\u6d3b\u751f\u4ea7\u8005\u534f\u7a0b\uff0c\u5e76\u7ee7\u7eed\u8fd9\u4e2afor\u5faa\u73af\u3002 \u5982\u4f55\u5728C\u8bed\u8a00\u4e2d\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\u800c\u4e0d\u9700\u4f7f\u7528\u6c47\u7f16\u4ee3\u7801\u53bbhack\u5806\u6808\u548c\u5bc4\u5b58\u5668\u5462\uff1f\u4ed6\u7ed9\u51fa\u7684\u6700\u521d\u7684\u5b9e\u73b0\u662f\u4f7f\u7528goto\u8bed\u53e5\u3002 int function(void) { static int i, state = 0; switch (state) { case 0: goto LABEL0; case 1: goto LABEL1; } &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,18],"tags":[50,16,35],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-note","category-18","tag-50","tag-16","tag-35"],"_links":{"self":[{"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/comments?post=445"}],"version-history":[{"count":2,"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/posts\/445\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/media?parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/categories?post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hawkwithwind.net\/blog\/wp-json\/wp\/v2\/tags?post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}