{"id":527,"date":"2013-12-27T23:39:00","date_gmt":"2013-12-27T15:39:00","guid":{"rendered":"http:\/\/note.systw.net\/note\/?p=527"},"modified":"2023-11-03T23:44:40","modified_gmt":"2023-11-03T15:44:40","slug":"c%e8%aa%9e%e8%a8%80-linux-thread","status":"publish","type":"post","link":"https:\/\/systw.net\/note\/archives\/527","title":{"rendered":"C\u8a9e\u8a00-Linux thread"},"content":{"rendered":"\n<p><strong>pthread<\/strong><br>POSIX thread\u70baPOSIX\u6a19\u6e96\u57f7\u884c\u7dd2.\u5b9a\u7fa9thread\u7684\u5efa\u7acb\u53ca\u4f7f\u7528\u65b9\u6cd5<br>pthread.h\u4e2d\u5e38\u7528\u7684\u6709:<br>pthread_create\u3001pthread_dispath\u3001pthread_mutex_lock(\u4e92\u65a5\u9396\u5b9a)\u3001pthread_mutex_unlock(\u4e92\u65a5\u89e3\u9396)<br>\u6240\u6709pthread.h\u53ef\u7528\u7684function\u53ef\u53c3\u8003 POSIX thread (pthread) libraries<br>http:\/\/www.yolinux.com\/TUTORIALS\/LinuxTutorialPosixThreads.html<\/p>\n\n\n\n<p>ps<br>\u82e5\u5728linux\u4e0bgcc\u7de8\u8b6f\uff0c\u9700\u52a0-lpthread\u53c3\u6578<br>ex:<br>gcc thread.c -o thread -lpthread<\/p>\n\n\n\n<p>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..<\/p>\n\n\n\n<p>\u5e38\u898b\u51fd\u6578\u8aaa\u660e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">pthread_create()<\/h2>\n\n\n\n<p>\u5efa\u7acbthread<br>\u51fd\u6578\u539f\u5f62\u5982\u4e0b<br><strong>int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);<\/strong><br>\u53c3\u6578\u8aaa\u660e\u5982\u4e0b<br><strong>pthread_t<\/strong>\u00a0*thread\uff1a thread id<br><strong>const pthread_attr_t *restrict attr<\/strong>\uff1athread\u5c6c\u6027<br><strong>void *(*start_routine) (void *)<\/strong>\uff1a\u6307\u5b9a\u8981\u505a\u70bathread\u7684function<br><strong>void *arg<\/strong>\uff1a\u7d66thread\u7684\u53c3\u6578<br>ps:<br>thread\u5efa\u7acb\u6210\u529f\u5f8c\uff0c\u51fd\u6578\u6703\u50b3\u56de0<br>\u82e5\u5efa\u7acb\u5931\u6557\u5247\u56de\u50b3\u932f\u8aa4\u4ee3\u78bc\uff0c\u5e38\u898b\u4ee3\u78bc\u70baEAGAIN\u548cEINVAL<br>EAGAIN\u8868\u793a\u5efa\u7acb\u7684thread\u6578\u91cf\u5df2\u8d85\u904e\u7cfb\u7d71\u9650\u5236\u7684thread\u6578\u91cf<br>EINVAL\u8868\u793athread\u5c6c\u6027\u4e0d\u6b63\u78ba<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex:<br>void printmsg(void);<br>.... omit ...<br>pthread_t tid;<br>pthread_create(&amp;tid, NULL, (void *) printmsg, NULL);<br>.... omit ...<br>ex<br>void *printmsg_a(void *arg);<br>void *printmsg_b(void *arg);<br>.... omit ...<br>pthread_t tid1, tid2;<br>pthread_create(&amp;tid1, NULL, &amp;printmsg_a, NULL);<br>pthread_create(&amp;tid2, NULL, &amp;printmsg_b, NULL);<br>.... omit ...<br>ex<br>void *printmsg(void *arg); \/\/argv&#91;1]\u4ee3\u5165arg<br>.... omit ...<br>pthread_t tid;<br>pthread_attr_t attr;<br>pthread_attr_init(&amp;attr);<br>char *argv&#91;];<br>pthread_create(&amp;tid, &amp;attr, printmsg, argv&#91;1]);<br>.... omit ...<br>ex:<br>void *printmsg( void *arg ){.... \/\/message\u4ee3\u5165arg<br>.... omit ...<br>pthread_t tid1, tid2;<br>const char *message1 = \"thread1\";<br>const char *message2 = \"thread2\";<br>iret1 = pthread_create( &amp;tid1, NULL, printmsg, (void*) message1);<br>iret2 = pthread_create( &amp;tid2, NULL, printmsg, (void*) message2);<br>.... omit ...<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><br>pthread_join()<\/h2>\n\n\n\n<p>\u7b49\u5f85\u6307\u5b9a\u7684thread\u7d50\u675f<br>\u51fd\u6578\u539f\u578b\u5982\u4e0b<br><strong>int pthread_join(pthread_t thread, void **thread_return);<\/strong><br>\u53c3\u6578\u8aaa\u660e\u5982\u4e0b<br><strong>pthread_t thread<\/strong>: thread id<br><strong>void **thread_return<\/strong>: \u7528\u4f86\u5132\u5b58thread\u7684\u56de\u50b3\u503c<br>ps:<br>\u7576\u7b49\u5f85\u6307\u5b9a\u7684thread\u7d50\u675f\u6642,\u8a72\u51fd\u6578\u6703\u88ab\u6536\u56de<br>ex:<br>pthread_join(id,NULL);<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">pthread_exit()<\/h2>\n\n\n\n<p>thread\u81ea\u5df1\u7d50\u675f<br>\u51fd\u6578\u539f\u578b\u5982\u4e0b<br><strong>void pthread_exit(void *retval);<\/strong><br>ex:<br>pthread_exit(0);<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">pthread_key_create<\/h2>\n\n\n\n<p>\u5efa\u7acbthread\u5168\u57df\u8b8a\u6578<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ex<br>#define NUMTHREADS 3<br>pthread_key_t glob_var_key;<br>pthread_t threads&#91;NUMTHREADS];<br>int i;<br>pthread_key_create(&amp;glob_var_key,NULL);<br>for (i=0; i &lt; NUMTHREADS; i++){<br>pthread_create(&amp;threads&#91;i],NULL,thread_func,NULL);<br>}<br>for (i=0; i &lt; NUMTHREADS; i++){<br>pthread_join(threads&#91;i], NULL);<br>}<br>...omit...<br>void* thread_func(void *arg){<br>pthread_setspecific(glob_var_key, &amp;glob_var);<br>...omit...<\/code><\/pre>\n\n\n\n<p><br>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.<\/p>\n\n\n\n<p>\u5e38\u898b\u7bc4\u4f8b<\/p>\n\n\n\n<p>\u986f\u793a\u8cc7\u8a0a<br><strong>ray@localhost# vi thread.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include<br>#include<br>void thread(void){<br>\u3000int i;<br>\u3000for(i=0;i&lt;2;i++)printf(\"This is a thread n\");<br>}<br>int main(void){<br>\u3000pthread_t id;<br>\u3000int i,ret;<br>\u3000pthread_create(&amp;id,NULL,(void *) thread,NULL);<br>\u3000for(i=0;i&lt;2;i++)printf(\"This is the process n\");<br>\u3000pthread_join(id,NULL);<br>\u3000return (0);<br>}<\/code><\/pre>\n\n\n\n<p><strong>ray@localhost# gcc thread.c -o thread -lpthread<br>ray@localhost# .\/thread<\/strong><br>This is the process<br>This is the process<br>This is a thread<br>This is a thread<br>ps<br>\u6bcf\u6b21\u7d50\u679c\u4e0d\u4e00\u6a23\uff0c\u9019\u662fthread\u548cmain process\u722d\u596aCPU\u8cc7\u6e90\u7684\u7d50\u679c<\/p>\n\n\n\n<p><br>\u8b80\u53d6\u5916\u90e8\u53c3\u6578\u505a\u8a08\u7b97<br><strong>ray@localhost# vi sum_by_thread.c<\/strong>\u3000<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include\n#include\nint sum;\nvoid *runner(void *param);\nint main(int argc, char *argv&#91;]){\n\u3000pthread_t tid;\n\u3000pthread_attr_t attr;\n\u3000pthread_attr_init(&amp;attr);\n\u3000pthread_create(&amp;tid,&amp;attr,runner,argv&#91;1]);\n\u3000pthread_join(tid,NULL);\n\u3000printf(\"sum=%dn\",sum);\n}\nvoid *runner(void *param){\n\u3000int i, upper= atoi(param);\n\u3000sum=0;\n\u3000if(upper>0){\n\u3000\u3000for(i=1,i&lt;=upper;i++)sum+=i;\n\u3000}\n\u3000pthread_exit(0);\n}<\/code><\/pre>\n\n\n\n<p><strong>ray@localhost# gcc sum_by_thread.c -o sum_by_thread -lpthread<br>ray@localhost# .\/sum_by_thread 3<\/strong><br>6<\/p>\n\n\n\n<p><br>\u5efa\u7acb\u4e09\u500bthread\u8b80\u53d6\u4e09\u500b\u6a94\u6848<br><strong>ray@localhost# vi wordthread.c<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include<br>#include<br>#include<br>#define MAX_WLEN 20 \/\/max length of a word<br>#define MAX_FLEN 6000 \/\/max word in a FILE<br>#define NUMTID 3 \/\/number of thread<br>\/\/\/\/\/\/\/\/\/\/\/thread<br>char wordall&#91;3]&#91;MAX_FLEN];<br>void rfile(char ta&#91;2]&#91;20]);<br>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br>int main(int argc, char *argv&#91;]){<br>\u3000int i;<br>\u3000\/\/\/\/\/\/\/\/\/\/\/\/\/ read file by thread<br>\u3000pthread_t tid&#91;NUMTID];<br>\u3000char tarray&#91;NUMTID]&#91;2]&#91;20];<br>\u3000char buffer&#91;1];<br>\u3000for(i=0;i&lt; NUMTID;i++){<br>\u3000\u3000sprintf(buffer, \"%d\", i);<br>\u3000\u3000strcpy(tarray&#91;i]&#91;0],buffer); \/\/save thread id<br>\u3000\u3000strcpy(tarray&#91;i]&#91;1],argv&#91;i+1]); \/\/save file path<br>\u3000\u3000pthread_create(&amp;tid&#91;i], NULL,(void *) rfile,tarray&#91;i]);<br>\u3000\u3000buffer&#91;0]=' 0 '; \/\/clear array<br>\u3000}<br>\u3000for(i=0;i&lt; NUMTID;i++){<br>\u3000\u3000pthread_join(tid&#91;i],NULL);<br>\u3000}<br>\u3000\/\/\/\/\/\/\/\/\/\/\/ collect word from all file<br>\u3000char word&#91;MAX_WLEN];<br>\u3000char *token;<br>\u3000char c;<br>\u3000int totalword=0;<br>\u3000for(i=0;i&lt; NUMTID;i++){<br>\u3000\u3000token = strtok(wordall&#91;i], \" ,.n\");<br>\u3000\u3000while(token){<br>\u3000\u3000\u3000strcpy(word, token);<br>\u3000\u3000\u3000for(c=0;c\u3000\u3000\u3000printf(\"%s n\", word);<br>\u3000\u3000\u3000totalword++;<br>\u3000\u3000\u3000token = strtok(NULL, \" ,.n\");<br>\u3000\u3000}<br>\u3000printf(\"total word is %d\", totalword);<br>\u3000}<br>}<br>\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br>\/\/\/\/\/\/\/\/\/\/\/read file in thread<br>void rfile(char ta&#91;2]&#91;20]){<br>\u3000FILE *aptr;<br>\u3000char line&#91;MAX_FLEN];<br>\u3000aptr = fopen(ta&#91;1],\"r\");<br>\u3000while(!feof(aptr)){<br>\u3000\u3000fgets(line, MAX_FLEN, aptr);<br>\u3000\u3000if(feof(aptr)) break;<br>\u3000\u3000strcpy(wordall&#91;atoi(ta&#91;0])], line);<br>\u3000}<br>\u3000fclose(aptr);<br>\u3000pthread_exit(0);<br>\u3000}<br>}<\/code><\/pre>\n\n\n\n<p><strong>ray@localhost# gcc wordthread.c -o wordthread -lpthread<\/strong><br><strong>ray@localhost# .\/wordthread text1 text2 text3<\/strong><br>system<br>linux<br>is<br>&#8230;omit..<br>total word is 9<\/p>\n\n\n\n<p><br>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<\/p>\n\n\n\n<p><strong>refer<\/strong><br>linux man-page http:\/\/man7.org\/linux\/man-pages\/man3\/<br>create a global variable http:\/\/stackoverflow.com\/questions\/15100824\/how-do-i-create-a-global-variable-that-is-thread-specific-in-c-using-posix-threa<br>Linux C\/C++\u591a\u7ebf\u7a0bpthread\u5b9e\u4f8b http:\/\/www.metsky.com\/archives\/550.html<br>Linux\u4e0b\u7684\u591a\u7dda\u7a0b\u7de8\u7a0b http:\/\/fanqiang.chinaunix.net\/a4\/b8\/20010811\/0905001105_b.html<br>\u53f0\u7063wiki pthread http:\/\/www.twwiki.com\/wiki\/Pthread<\/p>\n","protected":false},"excerpt":{"rendered":"<p>pthreadPOSIX thread\u70baPOSIX\u6a19\u6e96\u57f7\u884c\u7dd2 &#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":[14],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","hentry","category-develop"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/527","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=527"}],"version-history":[{"count":0,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"wp:attachment":[{"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/systw.net\/note\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}