• <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 多線程

            多線程

            編寫多線程程序時,在設計上要特別小心.
            對共享變量,多個執行路徑,要引起足夠重視.

            創建多線程:
            /*
             * 多線程
             
            */
            #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);
            }

            執行結果:
            [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

            從執行結果里,很顯然看到有bug,5個線程的argument全是5.
            因為新線程的參數,是使用地址引用傳遞的:
            res = pthread_create(&(a_thread[lots_of_threads]),NULL,
                            thread_function, (void *)&lots_of_threads);
            主線程創建線程循環,很快執行完. 引用地址中的值,在子線程執行前,已經被改成了5.
            線程參數改成值傳遞就好了.

            --
            FROM:Linux程序設計

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

            導航

            統計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久这里只有精品首页| 久久精品国产亚洲Aⅴ香蕉| 性色欲网站人妻丰满中文久久不卡| 久久精品免费一区二区| 人妻无码中文久久久久专区| 国产午夜精品理论片久久影视| av无码久久久久久不卡网站| 久久久久国产精品嫩草影院 | 久久久久国色AV免费看图片| 热久久最新网站获取| 99re这里只有精品热久久| 久久99精品久久久久久野外| 色8久久人人97超碰香蕉987| 久久久久久亚洲精品无码| AV无码久久久久不卡网站下载| 中文字幕精品久久久久人妻| 中文字幕亚洲综合久久| 日本人妻丰满熟妇久久久久久| 精品久久久久久久中文字幕| 国产成人久久AV免费| 波多野结衣久久一区二区| 精品视频久久久久| 久久综合久久综合久久| 久久精品人人做人人妻人人玩| 波多野结衣久久一区二区| 思思久久99热只有频精品66| 99久久免费只有精品国产| 久久99国产精品一区二区| 伊人久久大香线蕉亚洲| 老男人久久青草av高清| 久久婷婷午色综合夜啪| 一本色道久久88综合日韩精品 | 无码精品久久久久久人妻中字| 日本久久久久久久久久| 久久伊人色| 亚洲&#228;v永久无码精品天堂久久| 国内精品久久久久影院免费| 狠狠色婷婷综合天天久久丁香 | 色诱久久av| 欧美日韩精品久久久久| 国产精品无码久久久久久|