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

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

Posted on 2008-04-30 16:45 MichaelCao 閱讀(998) 評論(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>
            欧美激情一区二区久久久| 欧美三区不卡| 国内伊人久久久久久网站视频| 亚洲一本大道在线| 一本色道久久综合亚洲二区三区| 欧美久色视频| 亚洲一区在线免费| 午夜精品一区二区三区在线播放| 国产日韩1区| 久久综合图片| 欧美日本韩国| 午夜久久久久久| 久久9热精品视频| 亚洲国产成人tv| 亚洲三级影院| 国产精品伦一区| 久久九九热re6这里有精品| 久久精品免费| a4yy欧美一区二区三区| 亚洲综合欧美| 亚洲电影免费观看高清| 亚洲精品在线看| 国产一区二区三区电影在线观看| 米奇777超碰欧美日韩亚洲| 欧美国产一区视频在线观看 | 亚洲女爱视频在线| 国产一区二区三区精品久久久| 男女精品网站| 欧美性生交xxxxx久久久| 久久国产毛片| 欧美精品一区二区久久婷婷| 欧美专区日韩专区| 欧美高清不卡在线| 久久国产精品久久w女人spa| 牛牛精品成人免费视频| 午夜视频在线观看一区| 嫩草伊人久久精品少妇av杨幂| 午夜精品久久久久久久蜜桃app| 久久精品成人| 性欧美办公室18xxxxhd| 欧美国产日韩xxxxx| 久久国产精品黑丝| 欧美日韩另类丝袜其他| 免费看亚洲片| 国产欧美在线观看一区| 日韩午夜免费视频| 最新国产成人av网站网址麻豆| 亚洲一区欧美一区| 一区二区三区黄色| 美日韩免费视频| 久久人人爽人人爽| 国产欧美日韩综合| 亚洲一区二区三区四区五区午夜 | 亚洲人成毛片在线播放女女| 午夜精品一区二区在线观看| 一区二区三区四区在线| 欧美jizzhd精品欧美喷水| 久久久之久亚州精品露出| 国产精品久久久一本精品| 亚洲精品色婷婷福利天堂| 在线精品一区二区| 久久天天狠狠| 老**午夜毛片一区二区三区| 国产日本欧美视频| 先锋影音网一区二区| 欧美一区二区在线看| 国产精品久久久久三级| 日韩小视频在线观看专区| 妖精成人www高清在线观看| 欧美激情第一页xxx| 欧美激情黄色片| 激情久久久久久| 夜夜嗨av一区二区三区四季av| 男女精品网站| 亚洲国产美女久久久久| 亚洲人成7777| 欧美激情无毛| 99精品国产99久久久久久福利| 亚洲美女淫视频| 欧美日本精品在线| av成人免费| 亚洲欧美日韩一区二区在线 | 久久精品日产第一区二区| 国产精品久久夜| 亚洲欧美另类在线| 久久久久久综合| 亚洲黄一区二区三区| 欧美激情综合五月色丁香小说| 亚洲人成毛片在线播放| 久久噜噜亚洲综合| 欧美激情自拍| 国产精品99久久久久久宅男| 国产精品久久夜| 久久久久久穴| 日韩午夜激情av| 久久久久五月天| 亚洲人成在线观看一区二区| 欧美日韩一区三区四区| 性欧美大战久久久久久久久| 欧美成人激情在线| 一区二区三区精品视频| 国产欧美日韩三区| 免费成人av在线| 亚洲先锋成人| 欧美激情91| 亚洲欧美日韩电影| 亚洲国产精品日韩| 国产精品一页| 欧美激情在线狂野欧美精品| 午夜精品久久久久久久99樱桃 | 欧美在线不卡视频| 亚洲国产欧美在线人成| 国产精品久久久久久久免费软件| 性色av一区二区三区| 亚洲日本一区二区| 久久九九免费| 亚洲欧美日韩一区二区三区在线 | 欧美日韩亚洲国产一区| 久久精品91久久香蕉加勒比| 国内免费精品永久在线视频| 欧美国产精品久久| 欧美在线首页| 亚洲午夜免费视频| 亚洲激情专区| 欧美成人高清| 久久精品中文字幕免费mv| 中文日韩欧美| 亚洲美女性视频| 亚洲国内精品| 韩日欧美一区二区三区| 国产精品欧美日韩一区二区| 欧美福利一区| 嫩草成人www欧美| 久久一区中文字幕| 久久精品在线| 欧美一区二区三区在线| 亚洲尤物视频网| 一本一本a久久| 亚洲美女在线看| 亚洲肉体裸体xxxx137| 欧美黄色片免费观看| 麻豆成人在线播放| 久久亚洲美女| 久久野战av| 久久夜色精品国产| 美女视频黄免费的久久| 久久夜色撩人精品| 美日韩精品视频| 欧美高潮视频| 亚洲高清中文字幕| 亚洲国产精品一区制服丝袜| 亚洲国产第一| 日韩午夜三级在线| 一区二区三区成人| 亚洲一本视频| 亚欧成人精品| 久久三级福利| 欧美成人在线免费观看| 欧美经典一区二区| 欧美日韩在线不卡一区| 欧美午夜在线视频| 国产偷国产偷亚洲高清97cao| 国产婷婷色综合av蜜臀av| 极品尤物一区二区三区| 亚洲欧洲一区二区天堂久久| 日韩天堂在线视频| 美女精品在线观看| 欧美久久久久免费| 国产精品国产福利国产秒拍| 国产精品欧美日韩| 伊人狠狠色j香婷婷综合| 亚洲人成高清| 午夜精品免费视频| 蜜桃av一区二区三区| 亚洲国内自拍| 亚洲综合电影| 美女视频网站黄色亚洲| 国产精品xxx在线观看www| 国产一区二区三区久久久| 亚洲欧洲日韩在线| 午夜亚洲伦理| 亚洲国产成人精品久久| 亚洲视频一区在线观看| 久久精品一二三| 欧美无砖砖区免费| 国内精品久久久久影院 日本资源 国内精品久久久久伊人av | 亚洲片区在线| 亚洲欧美日韩成人高清在线一区| 久久偷窥视频| 国产精品系列在线播放| 亚洲国产精彩中文乱码av在线播放 | 午夜精品久久久久久久99黑人| 久久亚洲视频| 国产精品久久久久久久久久妞妞| 狠狠色丁香久久综合频道 | 国产一区二区三区在线观看网站 | 国产综合视频| 宅男66日本亚洲欧美视频| 可以免费看不卡的av网站| 一本色道久久综合狠狠躁篇怎么玩|