• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小默

            thread 多線程

            多線程

            編寫多線程程序時(shí),在設(shè)計(jì)上要特別小心.
            對(duì)共享變量,多個(gè)執(zhí)行路徑,要引起足夠重視.

            創(chuàng)建多線程:
            /*
             * 多線程
             
            */
            #include 
            <stdio.h>
            #include 
            <unistd.h>
            #include 
            <stdlib.h>
            #include 
            <pthread.h>

            #define NUM_THREADS 6

            void *thread_function(void *arg);

            int main(){
                
            int res;
                pthread_t a_thread[NUM_THREADS];
                
            void *thread_result;
                
            int lots_of_threads;

                
            for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++){
                    printf(
            "before pthread_create, lots_of_threads=%d\n",lots_of_threads);
                    res 
            = pthread_create(&(a_thread[lots_of_threads]),NULL,
                            thread_function, (
            void *)&lots_of_threads);
                    
            if(res != 0){
                        perror(
            "Thread creation failed");
                        exit(EXIT_FAILURE);
                    }
                }

                printf(
            "Waiting for threads to finish\n");

                
            for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--){
                    res 
            = pthread_join(a_thread[lots_of_threads], &thread_result);
                    
            if(res == 0){
                        perror(
            "Picked up a thread\n");
                    }
                    
            else{
                        perror(
            "pthread_join failed\n");
                    }
                }

                printf(
            "All done\n");
                exit(EXIT_SUCCESS);
            }

            void *thread_function(void *arg){
                
            int my_number = *(int *)arg;
                
            int rand_num;

                printf(
            "thread_funcion is running. Argument was %d\n", my_number);
                rand_num 
            = 1+(int)(9.0*rand()/(RAND_MAX+1.0));
                sleep(rand_num);
                printf(
            "Bye from %d\n", my_number);
                pthread_exit(NULL);
            }

            執(zhí)行結(jié)果:
            [green@colorfulgreen ch11]$ gcc -D_REENTRANT thread8.c -o thread8 -lpthread
            [green@colorfulgreen ch11]$ .
            /thread8    
            before pthread_create, lots_of_threads
            =0
            before pthread_create, lots_of_threads
            =1
            before pthread_create, lots_of_threads
            =2
            before pthread_create, lots_of_threads
            =3
            before pthread_create, lots_of_threads
            =4
            before pthread_create, lots_of_threads
            =5
            Waiting 
            for threads to finish
            thread_funcion 
            is running. Argument was 5
            thread_funcion 
            is running. Argument was 5
            thread_funcion 
            is running. Argument was 5
            thread_funcion 
            is running. Argument was 5
            thread_funcion 
            is running. Argument was 5
            thread_funcion 
            is running. Argument was 5
            Bye from 
            5
            Bye from 
            5
            Picked up a thread
            : Success
            Bye from 
            5
            Picked up a thread
            : Success
            Bye from 
            5
            Picked up a thread
            : Success
            Bye from 
            5
            Picked up a thread
            : Success
            Bye from 
            5
            Picked up a thread
            : Success
            Picked up a thread
            : Success
            All done

            從執(zhí)行結(jié)果里,很顯然看到有bug,5個(gè)線程的argument全是5.
            因?yàn)樾戮€程的參數(shù),是使用地址引用傳遞的:
            res = pthread_create(&(a_thread[lots_of_threads]),NULL,
                            thread_function, (void *)&lots_of_threads);
            主線程創(chuàng)建線程循環(huán),很快執(zhí)行完. 引用地址中的值,在子線程執(zhí)行前,已經(jīng)被改成了5.
            線程參數(shù)改成值傳遞就好了.

            --
            FROM:Linux程序設(shè)計(jì)

            posted on 2011-06-14 20:27 小默 閱讀(419) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Linux

            導(dǎo)航

            統(tǒng)計(jì)

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評(píng)論

            閱讀排行榜

            无码精品久久一区二区三区| 亚洲精品无码专区久久同性男| 国产精品久久久久久福利69堂| 四虎国产精品免费久久5151| 狠狠色丁香婷婷综合久久来来去 | 久久99国产精品一区二区| 91麻豆精品国产91久久久久久| 无码乱码观看精品久久| MM131亚洲国产美女久久| 免费精品国产日韩热久久| 91精品国产高清久久久久久io | 亚洲精品午夜国产va久久| 99久久精品国内| 亚洲国产精品无码久久久不卡 | 久久精品国产99国产精品澳门| 四虎亚洲国产成人久久精品| 久久成人影院精品777| 亚洲精品白浆高清久久久久久 | 亚洲AⅤ优女AV综合久久久| 精品免费tv久久久久久久| 久久国产精品77777| 精品久久久中文字幕人妻| 久久久久久久91精品免费观看| 亚洲国产精品人久久| 久久99久久99小草精品免视看| 亚洲AV无一区二区三区久久| 伊人久久久AV老熟妇色| 亚洲国产视频久久| 无码任你躁久久久久久老妇App| 国内精品伊人久久久久网站| 国产A级毛片久久久精品毛片| 中文字幕亚洲综合久久2| 精品久久久久久久久中文字幕| 国内精品久久久久久野外| 久久亚洲精品中文字幕三区| 欧美久久综合性欧美| 亚洲国产精品人久久| 久久人人爽人人爽人人片AV东京热| 伊人久久大香线蕉精品| 久久午夜无码鲁丝片午夜精品| 久久综合色老色|