{"id":4065,"date":"2018-08-25T09:22:11","date_gmt":"2018-08-25T01:22:11","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4065"},"modified":"2018-08-25T09:22:11","modified_gmt":"2018-08-25T01:22:11","slug":"collect%e7%90%86%e8%a7%a3%e5%8f%af%e9%87%8d%e5%85%a5%e4%b8%8e%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4065.html","title":{"rendered":"[collect]\u7406\u89e3\u53ef\u91cd\u5165\u4e0e\u7ebf\u7a0b\u5b89\u5168"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u6709\u4e9b\u6982\u5ff5\u8bb0\u5f97\u4e0d\u6e05\u695a\uff0c\u7ecf\u5e38\u6df7\u6dc6\uff0c\u8fd8\u662f\u56e0\u4e3a\u6ca1\u6709\u5b9e\u9645\u9047\u5230\u5e76\u5904\u7406\u8fc7\uff0c\u5148\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u67e5\u627e\u65b9\u4fbf\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><strong># \u53ef\u91cd\u5165\u51fd\u6570<\/strong><\/h6>\n<p><span style=\"color: #ff0000;\">\u53ef\u91cd\u5165\u51fd\u6570\uff0c\u4e0e\u591a\u7ebf\u7a0b\u65e0\u5173\uff0c\u5373\u53ef\u91cd\u5165\u6982\u5ff5\u5e76\u4e0d\u4f9d\u8d56\u4e8e\u591a\u7ebf\u7a0b\uff0c\u53ef\u91cd\u5165\u7684\u63d0\u51fa\u662f\u4f9d\u636e\u5355\u4e00\u7ebf\u7a0b\u63d0\u51fa\u6765\u7684\uff0c\u5f53\u7136\uff0c\u591a\u7ebf\u7a0b\u53ef\u91cd\u5165\u662f\u4ed6\u7684\u6269\u5c55\u3002<\/span><strong>\u4e00\u4e2a\u51fd\u6570\u88ab\u540c\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u75282\u6b21\u4ee5\u4e0a\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u5177\u6709\u53ef\u518d\u73b0\u6027(\u591a\u6b21\u8c03\u7528\u51fd\u6570\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u6837\u7684)\u3002\u90a3\u4e48\u6211\u4eec\u8bf4\u8fd9\u4e2a\u51fd\u6570\u662f\u53ef\u91cd\u5165\u7684\u3002<\/strong><\/p>\n<p>\u53ef\u91cd\u5165\uff0c\u5e76\u4e0d\u4e00\u5b9a\u8981\u662f\u591a\u7ebf\u7a0b\u7684\u3002<span style=\"color: #ff0000;\"><strong>\u53ef\u91cd\u5165\u53ea\u5173\u6ce8\u4e00\u4e2a\u7ed3\u679c\u53ef\u518d\u73b0\u6027\u3002<\/strong><\/span>\u5728APUE\u4e2d\uff0c\u53ef\u51fd\u6570\u53ef\u91cd\u5165\u7684\u6982\u5ff5\u6700\u5148\u662f\u5728\u8bb2signal\u7684handler\u7684\u65f6\u5019\u63d0\u51fa\u7684\u3002\u6b64\u65f6\u8fdb\u7a0b(\u7ebf\u7a0b)\u6b63\u5728\u6267\u884c\u51fd\u6570fun()\uff0c\u5728\u51fd\u6570fun()\u8fd8\u672a\u6267\u884c\u5b8c\u7684\u65f6\u5019\uff0c\u7a81\u7136\u8fdb\u7a0b\u63a5\u6536\u5230\u4e00\u4e2a\u4fe1\u53f7sig, \u6b64\u65f6\uff0c\u9700\u8981\u6682\u505c\u6267\u884cfun(),\u8981\u8f6c\u800c\u6267\u884csig\u4fe1\u53f7\u7684\u5904\u7406\u51fd\u6570sig_handler()\uff0c\u90a3\u4e48\uff0c\u5982\u679c\u5728sig_handler()\u4e2d\uff0c\u4e5f\u6070\u597d\u8c03\u7528\u4e86\u51fd\u6570fun().\u4fe1\u53f7\u7684\u5904\u7406\u662f\u4ee5\u8f6f\u7ec8\u7aef\u7684\u5f62\u5f0f\u8fdb\u884c\u7684\uff0c\u90a3\u4e48\uff0c\u5f53sig_handler()\u6267\u884c\u5b8c\u8fd4\u56de\u4e4b\u540e\uff0cCPU\u4f1a\u7ee7\u7eed\u4ecefun()\u88ab\u6253\u65ad\u7684\u5730\u65b9\u5f80\u4e0b\u6267\u884c\u3002\u8fd9\u91cc\u8bb2\u7684\u6bd4\u8f83\u7279\u6b8a\uff0c\u6700\u597d\u7684\u60c5\u51b5\u662f\uff0c\u8fdb\u7a0b\u4e2d\u8c03\u7528\u4e86fun()\uff0c\u51fd\u6570\uff0c\u4fe1\u53f7\u5904\u7406\u51fd\u6570sig_handle()\u4e2d\u4e5f\u8c03\u7528\u4e86fun()\u3002\u5982\u679cfun()\u51fd\u6570\u662f\u53ef\u91cd\u5165\u7684\uff0c\u90a3\u4e48\uff0c\u591a\u6b21\u8c03\u7528fun()\u51fd\u6570\u5c31\u5177\u6709\u53ef\u518d\u73b0\u6027\u3002\u4ece\u800c\uff0c\u4e24\u6b21\u8c03\u7528fun()\u7684\u7ed3\u679c\u662f\u6b63\u786e\u7684\u9884\u671f\u7ed3\u679c\u3002\u975e\u53ef\u91cd\u5165\u51fd\u6570\uff0c\u5219\u6070\u597d\u76f8\u53cd\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u7b80\u800c\u8a00\u4e4b\uff0c\u53ef\u91cd\u5165\u51fd\u6570\uff0c\u63cf\u8ff0\u7684\u662f\u51fd\u6570\u88ab\u591a\u6b21\u8c03\u7528\u4f46\u662f\u7ed3\u679c\u5177\u6709\u53ef\u518d\u73b0\u6027\u3002<\/strong><\/span><\/p>\n<p>\u5982\u679cfun()\uff0c\u4e2d\uff0c\u4f7f\u7528\u4e86static\u53d8\u91cf\u3001\u8fd4\u56de\u5168\u5c40\u53d8\u91cf\u3001\u8c03\u7528\u975e\u53ef\u91cd\u5165\u51fd\u6570\u7b49\u7b49\uff0c\u5e26\u6709\u5168\u5c40\u6027\u7684\u64cd\u4f5c\uff0c\u90fd\u5c06\u4f1a\u5bfc\u81f42\u6b21\u4ee5\u4e0a\u8c03\u7528fun()\u7684\u7ed3\u679c\u7684\u4e0d\u53ef\u518d\u73b0\u6027\uff08\u5f53\u7136\uff0c\u6709\u4e9b\u65f6\u5019\u4f7f\u7528\u4e86static\u3001\u5168\u5c40\u53d8\u91cf\u7b49\u7b49\uff0c\u4e0d\u4e00\u5b9a\u5bfc\u81f4\u8c03\u7528\u7ed3\u679c\u4e0d\u53ef\u518d\u73b0\u6027\uff09\u3002<span style=\"color: #ff0000;\"><strong>\u53ea\u8981\u4f7f\u8c03\u7528\u7ed3\u679c\u5177\u6709\u53ef\u518d\u73b0\u6027\uff0c\u90a3\u4e48\u8be5\u51fd\u6570\u5c31\u662f\u53ef\u91cd\u5165\u7684\u3002<\/strong><\/span><\/p>\n<p>\u4e3a\u4e86\u4fdd\u8bc1\u51fd\u6570\u662f\u53ef\u91cd\u5165\u7684\uff0c\u9700\u8981\u505a\u5230\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ol>\n<li>\u4e0d\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u9759\u6001\u6216\u8005\u5168\u5c40\u6570\u636e\uff1b<\/li>\n<li>\u4e0d\u8fd4\u56de\u9759\u6001\u6216\u8005\u5168\u5c40\u6570\u636e\uff0c\u6240\u6709\u7684\u6570\u636e\u90fd\u7531\u51fd\u6570\u8c03\u7528\u8005\u63d0\u4f9b\uff1b<\/li>\n<li>\u4f7f\u7528\u672c\u5730\u6570\u636e\uff0c\u6216\u8005\u901a\u8fc7\u5236\u4f5c\u5168\u5c40\u6570\u636e\u7684\u672c\u5730\u62f7\u8d1d\u6765\u4fdd\u62a4\u5168\u5c40\u6570\u636e\uff1b<\/li>\n<li>\u5982\u679c\u5fc5\u987b\u8bbf\u95ee\u5168\u5c40\u6570\u636e\uff0c\u4f7f\u7528\u4e92\u65a5\u9501\u6765\u4fdd\u62a4\uff1b<\/li>\n<li>\u4e0d\u8c03\u7528\u4e0d\u53ef\u91cd\u5165\u51fd\u6570\uff1b<\/li>\n<\/ol>\n<hr \/>\n<h6><strong># \u7ebf\u7a0b\u5b89\u5168<\/strong><\/h6>\n<blockquote><p>If a function can be safely called by multiple threads at the same time, we say that the function is thread-safe.<\/p><\/blockquote>\n<p>\u4e0a\u9762\u4e00\u6bb5\u8bdd\u662fAPUE\u4e2d\u7684\u89e3\u91ca\uff0c<span style=\"color: #ff0000;\"><strong>\u5982\u679c\u4e00\u4e2a\u51fd\u6570\u80fd\u591f\u5b89\u5168\u7684\u540c\u65f6\u88ab\u591a\u4e2a\u7ebf\u7a0b\u8c03\u7528\u800c\u5f97\u5230\u6b63\u786e\u7684\u7ed3\u679c\uff0c\u90a3\u4e48\uff0c\u6211\u4eec\u8bf4\u8fd9\u4e2a\u51fd\u6570\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/strong><\/span>\u6240\u8c13\u5b89\u5168\uff0c\u4e00\u5207\u53ef\u80fd\u5bfc\u81f4\u7ed3\u679c\u4e0d\u6b63\u786e\u7684\u56e0\u7d20\u90fd\u662f\u4e0d\u5b89\u5168\u7684\u8c03\u7528\u3002<\/p>\n<p>\u7ebf\u7a0b\u5b89\u5168\uff0c\u662f\u9488\u5bf9\u591a\u7ebf\u7a0b\u800c\u8a00\u7684\u3002\u90a3\u4e48\u548c\u53ef\u91cd\u5165\u8054\u7cfb\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u65ad\u5b9a\uff0c<span style=\"color: #ff0000;\"><strong>\u53ef\u91cd\u5165\u51fd\u6570\u5fc5\u5b9a\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4f46\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4e0d\u4e00\u5b9a\u662f\u53ef\u91cd\u5165\u7684\u3002<\/strong><\/span>\u4e0d\u53ef\u91cd\u5165\u51fd\u6570\uff0c\u51fd\u6570\u8c03\u7528\u7ed3\u679c\u4e0d\u5177\u6709\u53ef\u518d\u73b0\u6027\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e92\u65a5\u9501\u7b49\u673a\u5236\uff0c\u4f7f\u4e4b\u80fd\u5b89\u5168\u7684\u540c\u65f6\u88ab\u591a\u4e2a\u7ebf\u7a0b\u8c03\u7528\uff0c\u90a3\u4e48\uff0c\u8fd9\u4e2a\u4e0d\u53ef\u91cd\u5165\u51fd\u6570\u5c31\u662f\u8f6c\u6362\u6210\u4e86\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168\uff0c\u63cf\u8ff0\u7684\u662f\u51fd\u6570\u80fd\u540c\u65f6\u88ab\u591a\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u8c03\u7528\uff0c\u5e76\u4e0d\u8981\u6c42\u8c03\u7528\u51fd\u6570\u7684\u7ed3\u679c\u5177\u6709\u53ef\u518d\u73b0\u6027\u3002<\/strong>\u4e5f\u5c31\u662f\u8bf4\uff0c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5141\u8bb8\u51fa\u73b0\u4e92\u76f8\u5f71\u54cd\u7684\u60c5\u51b5\uff0c\u8fd9\u79cd\u60c5\u51b5\u7684\u51fa\u73b0\u9700\u8981\u67d0\u4e9b\u673a\u5236\u6bd4\u5982\u4e92\u65a5\u9501\u6765\u652f\u6301\uff0c\u4f7f\u4e4b\u5b89\u5168\u3002<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/blog.csdn.net\/feiyinzilgd\/article\/details\/5811157\">\u6df1\u5165\u7406\u89e3\u53ef\u91cd\u5165\u4e0e\u7ebf\u7a0b\u5b89\u5168<\/a>#nice<\/li>\n<li><a href=\"https:\/\/mp.weixin.qq.com\/s\/HkUQRWV1CF6kcZvushtJWw\">\u53ef\u91cd\u5165\u3001\u7ebf\u7a0b\u5b89\u5168\u548c\u5f02\u6b65\u4fe1\u53f7\u5b89\u5168<\/a>#nice<\/li>\n<li><a href=\"https:\/\/mp.weixin.qq.com\/s\/hxZoR7AO95K3jUf8uNSpKQ\">\u4ec0\u4e48\u662f\u7ebf\u7a0b\u5b89\u5168\uff1f\u5982\u4f55\u907f\u514d\uff1f<\/a><\/li>\n<li><a href=\"http:\/\/www.jasongj.com\/java\/thread_safe\/\">Java\u8fdb\u9636\uff08\u4e8c\uff09\u5f53\u6211\u4eec\u8bf4\u7ebf\u7a0b\u5b89\u5168\u65f6\uff0c\u5230\u5e95\u5728\u8bf4\u4ec0\u4e48<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6709\u4e9b\u6982\u5ff5\u8bb0\u5f97\u4e0d\u6e05\u695a\uff0c\u7ecf\u5e38\u6df7\u6dc6\uff0c\u8fd8\u662f\u56e0\u4e3a\u6ca1\u6709\u5b9e\u9645\u9047\u5230\u5e76\u5904\u7406\u8fc7\uff0c\u5148\u6574\u7406\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u67e5\u627e\u65b9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1202,1203],"class_list":["post-4065","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-reentrance","tag-thread-safe"],"views":3304,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4065"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4065\/revisions"}],"predecessor-version":[{"id":4080,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4065\/revisions\/4080"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}