• <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>
                覺(jué)得昨天的思考似乎還是不怎么過(guò)癮,有些問(wèn)題還不是很清楚.到底應(yīng)用方面兩個(gè)有什么區(qū)別呢?
            自己學(xué)著別人小小的動(dòng)了下手.
            先貼信號(hào)量的代碼.
            #include<pthread.h>
            #include<stdio.h>
            #include<sys/time.h>

            #define MAX 10
            pthread_t thread[2];
            pthread_mutex_t mut;
            int number=0,i;

            void * thread1()
            {
                printf("thread1: I'm thread 1 \n");
                for(i =0;i<MAX ;i++)
                {
                    printf("thread 1: number=%d \n",number);
                    pthread_mutex_lock(&mut);
                    number++;
                    pthread_mutex_unlock(&mut);
                    sleep(2);
                }
                printf("thread1: 主函數(shù)在等我完成任務(wù)嗎?\n");
                pthread_exit(NULL);
            }
            void *  thread2()
            {
                printf("thread2: I'm thread 2 \n");
                for(i =0; i<MAX;i++)
                {
                    printf("thread2 : number=%d\n",number);
                    pthread_mutex_lock(&mut);
                    number++;
                    pthread_mutex_unlock(&mut);
                    sleep(3);
                }
                printf("thread2 : 主函數(shù)在等我完成任務(wù)么?\n");
                pthread_exit(NULL);

            }

            void thread_create(void)
            {
                /*創(chuàng)建線程*/
                pthread_create(&thread[0],NULL,thread1,NULL);
                printf("線程1被創(chuàng)建!\n");
                pthread_create(&thread[1],NULL,thread2,NULL);
                printf("線程2被創(chuàng)建!\n");
            }
            void thread_wait(void)
            {
                /*等待線程結(jié)束*/
                pthread_join(thread[0],NULL);
                printf("線程1已經(jīng)結(jié)束!\n");
                pthread_join(thread[1],NULL);
                printf("線程2已經(jīng)結(jié)束!\n");
            }
            int main()
            {
                /*用默認(rèn)屬性初始化互斥鎖*/
                pthread_mutex_init(&mut,NULL);
                printf("我是主函數(shù),我正在創(chuàng)建線程!\n");
                thread_create();
                printf("我是主函數(shù),我正在等待線程完成任務(wù)!\n");
                thread_wait();
            }

            執(zhí)行的結(jié)果是:
            我是主函數(shù),我正在創(chuàng)建線程!
            thread1: I'm thread 1
            thread 1: number=0
            線程1被創(chuàng)建!
            thread2: I'm thread 2
            thread2 : number=1
            線程2被創(chuàng)建!
            我是主函數(shù),我正在等待線程完成任務(wù)!
            thread 1: number=2
            thread2 : number=3
            thread 1: number=4
            thread 1: number=5
            thread2 : number=6
            thread 1: number=7
            thread2 : number=8
            thread 1: number=9
            thread2 : number=10
            thread1: 主函數(shù)在等我完成任務(wù)嗎?
            線程1已經(jīng)結(jié)束!
            thread2 : 主函數(shù)在等我完成任務(wù)么?
            線程2已經(jīng)結(jié)束!

             重要:這個(gè)執(zhí)行的過(guò)程大概要10秒!!!!!!
            而我們用自旋鎖,代碼:
            /*
             * time :2008.4.30
             * author:will cao
             * Email:sei_michael@126.com
             * 探索自旋鎖與信號(hào)量的區(qū)別
             */
            #include<pthread.h>
            #include<stdio.h>

            pthread_t thread[2];
            pthread_spinlock_t lock ;

            #define MAX 10

            int number=0,i;

            void * thread1()
            {
                printf ("thread 1 :I began to run !");
                for(i=0;i<MAX;i++)
                {
                    printf("thread 1 :number=%d \n",number);
                    pthread_spin_lock(&lock);
                    number++;
                    pthread_spin_unlock(&lock);
                }
                printf("ok ,I am over !\n");
                pthread_exit(NULL);
            }
            void * thread2 ()
            {
                printf("thread2 : I start !!!\n");
                for(i=0;i<MAX;i++)
                {
                    printf("thread2 : number = %d \n",number);
                    pthread_spin_lock(&lock);
                    number++;
                    pthread_spin_unlock(&lock);
                }
                printf("thread 2: I am over!!!");
                pthread_exit(NULL);
            }

            void thread_create(void)
            {
                /*create the threads */
                pthread_create(&thread[0],NULL,thread1,NULL);
                printf("create the thread 1\n ");
                pthread_create(&thread[1],NULL,thread2,NULL);
                printf("create the thread 2 \n");
            }
            void thread_wait(void )
            {
                /*wait for the thread to be over */
                pthread_join(thread[0],NULL);
                printf("the thread 1 is over !\n");
                pthread_join(thread[1],NULL);
                printf("the thread 2 is over ! \n");
            }
            int main()
            {
                /* init the spin lock */
                pthread_spin_init(&lock,0);
                printf("i am the main,and I am creating the threads ");
                thread_create();
                printf("i am the main,and I am wait for the thread to be over!");
                thread_wait();
            }
             執(zhí)行結(jié)果為:
            i am the main,and I am creating the threads thread 1 :I began to run !thread 1 :number=0
            thread 1 :number=1
            thread 1 :number=2
            thread 1 :number=3
            thread 1 :number=4
            thread 1 :number=5
            thread 1 :number=6
            thread 1 :number=7
            thread 1 :number=8
            thread 1 :number=9
            ok ,I am over !
            create the thread 1
             thread2 : I start !!!
            create the thread 2
            i am the main,and I am wait for the thread to be over!thread2 : number = 10
            thread2 : number = 11
            thread2 : number = 12
            thread2 : number = 13
            thread2 : number = 14
            thread2 : number = 15
            thread2 : number = 16
            thread2 : number = 17
            thread2 : number = 18
            thread2 : number = 19
            thread 2: I am over!!!the thread 1 is over !
            the thread 2 is over !
               執(zhí)行時(shí)間:我沒(méi)用系統(tǒng)調(diào)用,但肯定是用不了0.1秒的...
            總結(jié):從表面上來(lái)看,很明顯的區(qū)別是當(dāng)我們用的是信號(hào)量的時(shí)候,這個(gè)時(shí)候是有調(diào)度的.因?yàn)閺倪\(yùn)行結(jié)果上來(lái)看,主線程在創(chuàng)建其他兩個(gè)線程后,其他線程開(kāi)始運(yùn)行.并且主線程也在運(yùn)行.但怎么運(yùn)行這個(gè)是無(wú)法確定的,這是一個(gè)并發(fā)的過(guò)程.
                當(dāng)使用自旋鎖后,這個(gè)就不一樣了.當(dāng)運(yùn)行到臨界區(qū)的時(shí)候,它是直接的過(guò)去,不是會(huì)產(chǎn)生一個(gè)等待,或者一個(gè)調(diào)度.
            不知道編譯器是怎么編譯的.很想知道編譯后二進(jìn)制代碼有什么區(qū)別.但這個(gè)好像有點(diǎn)太難....不過(guò)我覺(jué)得從運(yùn)行結(jié)果上來(lái)看這么多,應(yīng)該差不多了.


            Feedback

            # re: 用代碼來(lái)思考自旋鎖和信號(hào)量  回復(fù)  更多評(píng)論   

            2008-09-19 10:01 by 郭石
            博主,你前面不是在用互斥鎖嗎?哪里用了信號(hào)量呢?

            # re: 用代碼來(lái)思考自旋鎖和信號(hào)量  回復(fù)  更多評(píng)論   

            2008-10-27 18:27 by mach
            如果把上面代碼中的sleep去掉,估計(jì)運(yùn)行時(shí)間也不會(huì)超過(guò)0.1秒 哈哈

            # re: 用代碼來(lái)思考自旋鎖和信號(hào)量  回復(fù)  更多評(píng)論   

            2009-03-02 11:51 by galilio
            只有代碼,沒(méi)有思考

            # re: 用代碼來(lái)思考自旋鎖和信號(hào)量  回復(fù)  更多評(píng)論   

            2009-06-02 17:34 by falconflying
            我覺(jué)得樓主是想當(dāng)然了,對(duì)于spinlock的確會(huì)出現(xiàn)你所說(shuō)的結(jié)果,但是也會(huì)出現(xiàn)mutex類似的結(jié)果,在我的機(jī)器上就出現(xiàn)了,redhat5 企業(yè)版
            另外,mutex出現(xiàn)10秒的原因是你使用了sleep。。。。。

            posts - 16, comments - 16, trackbacks - 0, articles - 0

            Copyright © MichaelCao

            色综合久久久久| 男女久久久国产一区二区三区| 国内精品伊人久久久久av一坑| 国产精品久久免费| 亚洲欧美国产日韩综合久久 | 91超碰碰碰碰久久久久久综合| 手机看片久久高清国产日韩| 97久久超碰国产精品2021| 久久午夜电影网| 无码国内精品久久人妻麻豆按摩| 亚洲性久久久影院| 97精品国产97久久久久久免费| 久久中文精品无码中文字幕| 亚洲综合精品香蕉久久网97| 久久人人爽人爽人人爽av| 久久久久久夜精品精品免费啦| 伊人久久综合精品无码AV专区 | 99久久精品影院老鸭窝| 久久影视综合亚洲| 一本久久久久久久| 国产亚洲精品自在久久| 最新久久免费视频| 国产精品热久久毛片| 国产三级精品久久| 激情伊人五月天久久综合| 无码人妻少妇久久中文字幕| 国产亚洲欧美成人久久片| 88久久精品无码一区二区毛片 | 婷婷国产天堂久久综合五月| 2021最新久久久视精品爱| 久久人妻无码中文字幕| 久久久无码精品亚洲日韩蜜臀浪潮 | 日韩精品久久久久久免费| 亚洲欧美成人久久综合中文网 | 国产精品久久久久久福利漫画| 亚洲色大成网站www久久九| 久久频这里精品99香蕉久| 久久这里只精品99re66| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久夜色tv网站| 国产一区二区三区久久|