{"id":712,"date":"2014-01-11T15:40:00","date_gmt":"2014-01-11T07:40:00","guid":{"rendered":"http:\/\/note.systw.net\/note\/?p=712"},"modified":"2023-11-04T15:44:37","modified_gmt":"2023-11-04T07:44:37","slug":"thread","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/712","title":{"rendered":"Thread"},"content":{"rendered":"\n<p><strong>thread(\u57f7\u884c\u7dd2)<\/strong><br>process\u4e2d\u7684\u4e00\u6bb5\u53ef\u57f7\u884c\u7269\u4ef6,<br>\u662f\u96fb\u8166\u4e2d\u6700\u5c0f\u7684\u57f7\u884c\u55ae\u4f4d,\u4e5f\u662f\u80fd\u5728cpu schedling\u4e2d\u7684\u57fa\u672c\u55ae\u4f4d<br>\u53ef\u5206\u70ba\u5169\u7a2e<br>\u3000mutli thread process<br>\u3000single thread process<\/p>\n\n\n\n<p><strong>mutli thread process<\/strong><br>\u4e00\u500bprocess\u591a\u500bthread<br>\u7d50\u69cb\u5982\u4e0b<br>[code,data,file]<br>+[register][stack][thread]<br>+[register][stack][thread]<br>+[register][stack][thread]<br>+[regis&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n\n\n\n<p><strong>single thread process<\/strong><br>\u4e00\u500bprocess\u4e00\u500bthread<br>\u7d50\u69cb\u5982\u4e0b<br>[code,data,file]<br>+[registers,stack][thread]<\/p>\n\n\n\n<p>ps:<br>code,data,file\u8868\u793a\u53ef\u5728thread\u4e2d\u5206\u4eab\u7684process\u7684\u8cc7\u6e90<br>ps:<br>static variable\u662f\u5728heap\u5340,\u4e5f\u5c31\u662f\u5728data\u4e2d<br>auto variable\u662f\u5728stack\u5340<\/p>\n\n\n\n<p>ps:<br>single process+single thread process,&nbsp;ex:ms-dos<br>multi process+single thread process,&nbsp;ex:tradition unix<br>single process+multithread process,&nbsp;ex:java virtual machine<br>multi process+multithread process,&nbsp;ex:win2000,solaris,linux,&#8230;\u7b49<\/p>\n\n\n\n<p>&#8230;<\/p>\n\n\n\n<p><strong>multithreading\u512a\u9ede<\/strong><br>\u4e3b\u8981\u6709\u4ee5\u4e0b<br><strong>responsiveness<\/strong>:\u7a0b\u5f0f\u53ef\u4ee5\u540c\u6642\u505a\u5f88\u591a\u4e8b<br>\u3000ex;word \u986f\u793a\u5716\u5f62,\u8f38\u5165\u8cc7\u6599,\u6587\u6cd5\u6aa2\u67e5<br><strong>resource shaing<\/strong>: \u76f8\u540cprocess\u7684thread\u5171\u4eabcode\u548cglobal\u8b8a\u6578\u7b49\u8cc7\u8a0a<br>\u3000ps:\u82e5\u662fprocess\u4e4b\u9593\u7684\u5171\u4eab\u9700\u900f\u904eIPC\u6a5f\u5236<br><strong>economy<\/strong>:\u4f7f\u7528\u7684\u8cc7\u6e90\u8f03\u5c11,thread switching\u6bd4context switching\u66f4\u7bc0\u7701\u8cc7\u6e90<br><strong>scalability<\/strong>:<br>\u3000\u53ef\u5728single-thread\u7684CPU\u4e0a\u57f7\u884c,<br>\u3000\u5728multithread\u7684CPU\u4e0a\u4e5f\u53ef\u4ee5\u6709\u5f88\u597d\u7684\u6548\u80fd,\u82e5\u5728\u591acore\u591athread\u6548\u80fd\u66f4\u597d<br>ps:<br>user thread\u53ea\u53ef\u5f97\u5230resource sharing,econmy\u4e8c\u500b\u512a\u9ede<\/p>\n\n\n\n<p>ps:<br><strong>\u5be6\u4f5c\u4e0a\u7684\u6311\u6230<\/strong><br>\u4e3b\u8981\u6709\u4ee5\u4e0b<br>\u3000dividing activities \u5c07\u5de5\u4f5c\u5e73\u5747\u5207\u958b\u4ee5\u5206\u914d\u7d66\u4e0d\u540c\u7684thread<br>\u3000balance<br>\u3000data splitting<br>\u3000data dependency<br>\u3000testing and debugging<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n\n\n\n<p><strong>thread level<\/strong><br>\u4f9d\u5c64\u7d1a\u5206\u70ba\u4ee5\u4e0b\u5169\u985e<br><strong>ULT(user-level thread)<\/strong><br>\u3000\u4f7f\u7528ULT library\u7ba1\u7406thread<br>\u3000\u4e0d\u6703\u4f7f\u7528\u5230system call<br>\u3000os\u67e5\u89ba\u4e0d\u5230thread\u7684\u5b58\u5728<br>\u3000ex:posix pthreads,win32 threads,java threads<br><strong>KLT(kernel-level thread)<\/strong><br>\u3000\u7531os\u7ba1\u7406thread<br>\u3000\u6703\u7528\u5230system call<br>\u3000ex:windows,solaris,linux,mac os x,tru64 unix<br>ps:<br>\u4e0a\u8ff0\u5169\u8005\u90fd\u7531user space\u57f7\u884c<br>ps:<br>kernel thread\u8868\u793akernel\u53ef\u4ee5\u63d0\u4f9bthread\u529f\u80fd\uff0c\u4e26\u4e0d\u4ee3\u8868kernel\u672c\u8eab\u662f\u8dd1multithread<\/p>\n\n\n\n<p><br>&#8230;&#8230;.<\/p>\n\n\n\n<p><strong>multithreading modes<\/strong><br>\u4e3b\u8981\u6709\u4ee5\u4e0b<br><strong>many-to-one model<\/strong><br>\u3000\u591a\u500buser thread \u5c0d\u61c91\u500bkernel thread<br>\u3000kernel\u53ea\u6703\u8a8d\u70ba\u53ea\u6709\u4e00\u500bthread,\u56e0\u70ba\u4e0d\u77e5\u4e0a\u5c64\u72c0\u6cc1<br>\u3000\u4e0d\u652f\u63f4parallel<br>\u3000entire process will blocked if a thread makes a blocking system call<br>\u3000\u901a\u5e38\u7528\u5728\u4e0d\u652f\u63f4multithread\u7684os<br>\u3000ex:solaris green threads,gun portable threads<br><strong>one-to-one model<\/strong><br>\u3000\u5404\u5225user thread \u5c0d\u61c9\u5404\u500bkernel thread<br>\u3000more concurrency, allowing another thread to run when a thread makes a blocking system call<br>\u3000\u9650\u5236thread \u6578\u91cf,\u56e0\u70ba\u4e00\u500bkernel thread\u6703\u4f54\u7528\u4e00\u90e8\u4efd\u8cc7\u6e90<br>\u3000kernel\u8a8d\u70ba\u6709\u591a\u500bthread<br>\u3000ex:win nt,xp,2000,linux,solaris9 and later<br><strong>many-to-many model<\/strong><br>\u3000\u591a\u500buser thread\u5c0d\u61c9\u8f03\u5c11kernel thread<br>\u3000kernl\u4e0d\u77e5\u4e0a\u5c64\u72c0\u6cc1<br>\u3000\u4e0d\u9650\u5236thread \u6578\u91cf<br>\u3000\u652f\u63f4parallel<br>\u3000ex:winnt\/2000<br><strong>two-level model<\/strong><br>\u3000ex:IRIX,HP-UX,tru64 UNIX,solaris8<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<\/p>\n\n\n\n<p><strong>thread libraries<\/strong><br>\u7528\u4f86\u5be6\u4f5cthread program,<br>\u4e3b\u8981\u6709\u5169\u7a2e<br>\u3000library entirely in user space<br>\u3000kernel-level library<\/p>\n\n\n\n<p><strong>pthreads<\/strong><br>\u4f9d\u64daieee 1003.1c\u6a19\u6e96\u5b9a\u7fa9,<br>POSIX stand API for thread<br>\u53ef\u7528\u5728user-level\u548ckernel-level<br>\u5e38\u7528\u5728\u4e00\u822cunix\u4f5c\u696d\u7cfb\u7d71<\/p>\n\n\n\n<p><strong>implicit threading<\/strong><br>thread pools<br>openMP<br>grand central dispatch<br>TBB(threading building blocks)<\/p>\n\n\n\n<p><strong>thread pool<\/strong><br>\u7522\u751f\u591a\u500bthread,\u4e26\u7b49\u5f85\u57f7\u884c<br>\u4f7f\u7528\u73fe\u6709thread\u6bd4\u65b0\u7522\u751f\u4e00\u500bthread\u5feb<br>\u540c\u6642\u7b49\u5f85\u57f7\u884c\u7684\u6578\u76ee\u524d\u4f9dthread pool\u5927\u5c0f\u6c7a\u5b9a<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n\n\n\n<p><strong>threading issue<\/strong><\/p>\n\n\n\n<p><strong>semantics of fork() and exec()<\/strong><br>fork\u547c\u53eb\u4e00\u500bthread\u6216all thread?<\/p>\n\n\n\n<p><strong>thread cancellation<\/strong><br>\u5206\u70ba\u4e09\u7a2e<br>\u3000asynchronous cancellation:\u7acb\u523b\u53d6\u6d88<br>\u3000deferred cancellation:\u4e00\u500b\u6642\u9593\u5468\u671f\u5230\u624d\u6703\u53d6\u6d88<br>\u3000actual cancellation:\u6839\u64dathread\u72c0\u614b\u53d6\u6d88<\/p>\n\n\n\n<p><strong>signal handling<\/strong><br>singal type<br>\u3000synchronous signal&nbsp;ex:illegal memory access,division by zero<br>\u3000asynchronous signal&nbsp;ex:crtl+c<br>signal handler:\u8ca0\u8cac\u8655\u7406signal<br>\u3000default \u7531\u7de8\u8f2f\u5668\u81ea\u52d5\u8f09\u5165\u9810\u8a2d\u7684\u8a0a\u865f\u8655\u7406<br>\u3000user-defined \u81ea\u884c\u5b9a\u7fa9\u8a0a\u865f\u7684\u8655\u7406<\/p>\n\n\n\n<p><strong>TLS(thread-local storage)<\/strong><br>\u5141\u8a31\u6bcf\u500bthread\u6709\u81ea\u5df1\u7684\u7368\u7acb\u7a7a\u9593<br>\u53ef\u89e3\u6c7a\u5982\u4f55\u5340\u5206\u90a3\u4e00\u500bthread\u662f\u8001\u5927\u8001\u4e8c\u8001\u4e09,<\/p>\n\n\n\n<p><strong>scheduler activations<\/strong><br>\u5728user-level thread library\u8207kernel\u4e4b\u9593\u901a\u8a0a\u7684\u6280\u5de7<\/p>\n\n\n\n<p><strong>LWP(light weight process)<\/strong><br>\u4ecb\u65bcuser-level thread\u548ckernel-level thread\u7684\u8cc7\u6599\u7d50\u69cb<br>\u7528\u4f86\u63d0\u4f9b\u4e00\u500bvirtual process\u8b93\u61c9\u7528\u7a0b\u5f0fthread\u505aschedule<br>\u652f\u63f4\u591a\u500bUser-level Thread\uff0c\u5c0d\u61c9\u5230\u4e00\u500bkernel thread<br>\u5c6c\u65bckernel-level thread<\/p>\n","protected":false},"excerpt":{"rendered":"<p>thread(\u57f7\u884c\u7dd2)process\u4e2d\u7684\u4e00\u6bb5\u53ef\u57f7\u884c\u7269\u4ef6,\u662f\u96fb &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[22],"tags":[],"class_list":["post-712","post","type-post","status-publish","format-standard","hentry","category-operationsystem"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/comments?post=712"}],"version-history":[{"count":0,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/712\/revisions"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}