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

            多線程

            編寫多線程程序時,在設計上要特別小心.
            對共享變量,多個執(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個線程的argument全是5.
            因為新線程的參數(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程序設計

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

            導航

            統(tǒng)計

            留言簿(13)

            隨筆分類(287)

            隨筆檔案(289)

            漏洞

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            久久午夜电影网| 精品久久久久久| 欧美午夜精品久久久久久浪潮| 91超碰碰碰碰久久久久久综合| 国产午夜电影久久| 噜噜噜色噜噜噜久久| 久久ZYZ资源站无码中文动漫| 亚洲嫩草影院久久精品| 伊人热热久久原色播放www| 久久精品国产亚洲AV嫖农村妇女| 66精品综合久久久久久久| 久久只有这里有精品4| 国产午夜精品久久久久免费视 | 亚洲va久久久噜噜噜久久| 国产精品久久波多野结衣| 久久本道伊人久久| 亚洲精品国产第一综合99久久| 综合久久国产九一剧情麻豆| 久久AⅤ人妻少妇嫩草影院| 无码久久精品国产亚洲Av影片 | 精品久久久久久中文字幕| 免费无码国产欧美久久18| 久久www免费人成精品香蕉| 国产成人精品久久二区二区| 久久精品卫校国产小美女| 欧美午夜A∨大片久久 | 国产精品99久久免费观看| 久久午夜免费视频| 免费一级做a爰片久久毛片潮| 亚洲国产精品一区二区久久| 97久久久精品综合88久久| 97精品依人久久久大香线蕉97| 一级做a爰片久久毛片毛片| 人妻少妇精品久久| 天天做夜夜做久久做狠狠| 66精品综合久久久久久久| 大美女久久久久久j久久| 国产免费久久精品99久久| 99久久亚洲综合精品网站| 国产福利电影一区二区三区久久久久成人精品综合 | 一级做a爰片久久毛片看看|