青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

用代碼來思考自旋鎖和信號量

Posted on 2008-04-30 16:45 MichaelCao 閱讀(988) 評論(4)  編輯 收藏 引用 所屬分類: OS
    覺得昨天的思考似乎還是不怎么過癮,有些問題還不是很清楚.到底應用方面兩個有什么區別呢?
自己學著別人小小的動了下手.
先貼信號量的代碼.
#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: 主函數在等我完成任務嗎?\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 : 主函數在等我完成任務么?\n");
    pthread_exit(NULL);

}

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

執行的結果是:
我是主函數,我正在創建線程!
thread1: I'm thread 1
thread 1: number=0
線程1被創建!
thread2: I'm thread 2
thread2 : number=1
線程2被創建!
我是主函數,我正在等待線程完成任務!
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: 主函數在等我完成任務嗎?
線程1已經結束!
thread2 : 主函數在等我完成任務么?
線程2已經結束!

 重要:這個執行的過程大概要10秒!!!!!!
而我們用自旋鎖,代碼:
/*
 * time :2008.4.30
 * author:will cao
 * Email:sei_michael@126.com
 * 探索自旋鎖與信號量的區別
 */
#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();
}
 執行結果為:
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 !
   執行時間:我沒用系統調用,但肯定是用不了0.1秒的...
總結:從表面上來看,很明顯的區別是當我們用的是信號量的時候,這個時候是有調度的.因為從運行結果上來看,主線程在創建其他兩個線程后,其他線程開始運行.并且主線程也在運行.但怎么運行這個是無法確定的,這是一個并發的過程.
    當使用自旋鎖后,這個就不一樣了.當運行到臨界區的時候,它是直接的過去,不是會產生一個等待,或者一個調度.
不知道編譯器是怎么編譯的.很想知道編譯后二進制代碼有什么區別.但這個好像有點太難....不過我覺得從運行結果上來看這么多,應該差不多了.


Feedback

# re: 用代碼來思考自旋鎖和信號量  回復  更多評論   

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

# re: 用代碼來思考自旋鎖和信號量  回復  更多評論   

2008-10-27 18:27 by mach
如果把上面代碼中的sleep去掉,估計運行時間也不會超過0.1秒 哈哈

# re: 用代碼來思考自旋鎖和信號量  回復  更多評論   

2009-03-02 11:51 by galilio
只有代碼,沒有思考

# re: 用代碼來思考自旋鎖和信號量  回復  更多評論   

2009-06-02 17:34 by falconflying
我覺得樓主是想當然了,對于spinlock的確會出現你所說的結果,但是也會出現mutex類似的結果,在我的機器上就出現了,redhat5 企業版
另外,mutex出現10秒的原因是你使用了sleep。。。。。

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

Copyright © MichaelCao

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产一区二区观看| 国产精品v欧美精品∨日韩| 久久国产免费| 欧美精品激情| 精品成人一区二区三区四区| 亚洲性线免费观看视频成熟| 欧美电影打屁股sp| 小黄鸭精品aⅴ导航网站入口 | 欧美韩日一区二区三区| 国产伦精品一区二区三区视频黑人 | 伊伊综合在线| 久久国产免费看| 亚洲图片你懂的| 欧美日韩另类国产亚洲欧美一级| 91久久嫩草影院一区二区| 久久综合一区二区三区| 午夜激情综合网| 国产日韩欧美精品在线| 久久xxxx| 欧美一区成人| 激情成人亚洲| 麻豆精品精品国产自在97香蕉| 午夜久久美女| 国产一区二区福利| 欧美成人dvd在线视频| 老牛国产精品一区的观看方式| 亚洲国产精品成人一区二区| 欧美国产高潮xxxx1819| 欧美福利影院| 亚洲视频每日更新| 亚洲嫩草精品久久| 狠狠综合久久| 亚洲国产精品成人| 久久综合狠狠综合久久综青草| 在线看日韩av| 最新国产精品拍自在线播放| 欧美日韩国产高清视频| 亚洲欧美日韩在线| 欧美一级专区免费大片| 麻豆精品精华液| 欧美午夜激情小视频| 久久精品国产99| 久久精品中文字幕一区二区三区 | 99re热精品| 久久亚洲国产成人| 久久日韩粉嫩一区二区三区| 欧美日韩免费观看一区| 91久久精品国产91性色tv| 亚洲国产精品毛片| 久久综合久久美利坚合众国| 久久九九全国免费精品观看| 国产午夜精品久久久| 欧美亚洲一区二区在线| 久久综合久久综合久久综合| 国产真实久久| 美国成人毛片| 日韩网站免费观看| 欧美一区二区三区播放老司机| 欧美日韩一区二区在线播放| 国产精品v欧美精品v日本精品动漫| 91久久在线| 欧美亚洲网站| 亚洲日韩欧美一区二区在线| 欧美日韩福利视频| 欧美一区二区三区免费看| 欧美成ee人免费视频| 一区二区欧美日韩视频| 国产日韩欧美在线观看| 免费精品视频| 亚洲尤物视频网| 亚洲国产综合视频在线观看| 午夜精品在线观看| 亚洲免费电影在线| 一区二区三区精品| 免费成人黄色av| 欧美在线视频二区| 亚洲午夜在线| 一区二区日韩伦理片| 欧美.日韩.国产.一区.二区| 亚洲一区视频在线观看视频| 欧美不卡视频一区发布| 久久国产视频网站| 国产精品99久久久久久久女警| 亚洲女人天堂av| 日韩亚洲欧美在线观看| 激情久久久久久久| 国产日韩精品久久| 国产日韩精品一区二区三区在线| 欧美日韩亚洲不卡| 国产精品jvid在线观看蜜臀| 欧美日韩国产高清| 欧美日韩午夜在线视频| 欧美视频在线观看免费网址| 欧美视频网址| 国产伦精品一区二区三区高清版 | 欧美一区二区三区日韩视频| 99亚洲精品| 在线综合欧美| 欧美在线精品免播放器视频| 久久视频在线视频| 欧美好骚综合网| 国产女主播视频一区二区| 在线看片日韩| 午夜激情亚洲| 亚洲福利在线观看| 亚洲一区二区高清| 久久人人爽人人| 亚洲免费高清| 久久久亚洲影院你懂的| 国产精品高潮呻吟久久av无限| 国色天香一区二区| 一本久道久久久| 国产日韩专区在线| 亚洲高清免费视频| 一区二区三欧美| 性高湖久久久久久久久| 狂野欧美一区| 夜夜精品视频| 久久精品综合一区| 最新亚洲视频| 久久免费99精品久久久久久| 欧美激情a∨在线视频播放| 国产精品va在线播放| 精品91在线| 午夜一级在线看亚洲| 免费欧美日韩| 午夜精品亚洲| 欧美日韩亚洲综合一区| 国模精品一区二区三区| 99综合视频| 欧美国产成人精品| 亚洲欧美一区二区视频| 日韩视频免费观看高清完整版| 久久国产精品亚洲77777| 亚洲精品女人| 羞羞视频在线观看欧美| 亚洲激情欧美激情| 久久精品国产免费观看| 亚洲自拍啪啪| 99国产精品自拍| 免费欧美视频| 欧美日韩免费一区二区三区| 伊人成年综合电影网| 免费在线看成人av| 亚洲一二三四久久| 欧美天堂亚洲电影院在线观看| 狠狠色狠狠色综合日日91app| 久久久久国产精品麻豆ai换脸| 亚洲在线日韩| 亚洲第一福利在线观看| 亚洲国产天堂网精品网站| 欧美激情一区二区在线| 一本大道久久a久久精品综合| 亚洲精品日本| 国产欧美短视频| 国产精品亚洲а∨天堂免在线| 香蕉尹人综合在线观看| 浪潮色综合久久天堂| 亚洲精品视频二区| 日韩午夜在线视频| 精品动漫一区| 夜夜爽av福利精品导航| 国产午夜精品一区二区三区欧美 | 亚洲精品看片| 国产日产欧美一区| 亚洲国产三级网| 国产欧美日韩一区二区三区在线观看| 久久久中精品2020中文| 欧美激情在线有限公司| 亚洲性线免费观看视频成熟| 久久久噜噜噜久久人人看| 亚洲视屏在线播放| 欧美大片在线看| 欧美超级免费视 在线| 欧美日韩午夜| 最新亚洲视频| 99精品国产热久久91蜜凸| 日韩亚洲一区二区| 亚洲激情欧美| 欧美mv日韩mv国产网站app| 久久超碰97中文字幕| 欧美日韩免费观看一区=区三区| 亚洲第一精品影视| 亚洲人成人一区二区三区| 精品成人在线| 蜜桃伊人久久| 99精品免费视频| 午夜精品在线看| 国产日韩欧美制服另类| 亚洲欧美视频一区| 久久久成人网| 欧美日韩精品一本二本三本| 99在线精品视频| 亚洲精品国产无天堂网2021| 欧美区亚洲区| 亚洲伊人观看| 欧美激情视频在线播放| 欧美成人一区二免费视频软件| 亚洲精品乱码久久久久久黑人 | 久久综合影音|