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

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

Posted on 2008-04-30 16:45 MichaelCao 閱讀(983) 評論(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>
            国产精品丝袜91| 欧美一区日本一区韩国一区| 一区二区三区产品免费精品久久75| 国产综合色精品一区二区三区| 国产精品videossex久久发布| 欧美日本免费一区二区三区| 欧美视频免费| 国产精品自在线| 狠狠色香婷婷久久亚洲精品| 在线免费日韩片| 亚洲精选视频免费看| 一区二区三区精品视频在线观看| 亚洲欧美日韩国产一区二区| 麻豆精品传媒视频| 亚洲成人在线网| 亚洲第一精品福利| 日韩一级成人av| 亚洲免费网址| 免费观看国产成人| 国产精品草草| 永久免费毛片在线播放不卡| 99pao成人国产永久免费视频| 午夜精品视频| 亚洲国产精品女人久久久| 在线天堂一区av电影| 久久视频国产精品免费视频在线 | 亚洲一区图片| 久久久xxx| 国产精品久久久久久户外露出| 国产一区激情| 亚洲一区二区成人| 欧美成人一区在线| 亚洲影院免费观看| 欧美激情精品久久久久久黑人 | 久久不射网站| 亚洲精品欧美一区二区三区| 久久精品网址| 国产伦精品一区二区三区| 亚洲精品国产精品久久清纯直播| 午夜亚洲激情| 亚洲裸体俱乐部裸体舞表演av| 久久久成人网| 国产一区视频在线观看免费| 亚洲在线国产日韩欧美| 亚洲精品美女久久7777777| 久久久av水蜜桃| 国产视频欧美| 欧美亚洲视频在线看网址| 亚洲精品欧美日韩| 欧美成人资源| 亚洲国产欧美一区| 欧美aa在线视频| 久久久精品国产免费观看同学| 国产精品伦子伦免费视频| 国产精品99久久久久久宅男| 最新国产乱人伦偷精品免费网站| 久久夜色精品国产欧美乱| 国产专区欧美专区| 久久―日本道色综合久久| 亚洲伊人观看| 国产人妖伪娘一区91| 午夜激情一区| 亚洲欧美一区二区三区极速播放| 欧美性jizz18性欧美| 亚洲视频一区二区| 亚洲视频1区| 国产精品入口福利| 久久都是精品| 国产欧美日韩不卡免费| 亚洲毛片av在线| 亚洲国产精品黑人久久久| 老司机精品视频一区二区三区| 激情丁香综合| 欧美刺激性大交免费视频 | 久久国产精品99国产精| 国产一区二区三区在线观看网站 | 一区二区三区视频免费在线观看| 亚洲精品乱码久久久久久按摩观| 欧美激情网站在线观看| 亚洲图片欧美日产| 亚洲欧美在线视频观看| 黄色成人小视频| 亚洲国产三级在线| 国产精品成人一区二区| 久久精品91久久久久久再现| 久久一区二区精品| 日韩午夜在线| 亚洲欧美韩国| 亚洲欧洲精品一区二区精品久久久| 亚洲精品美女91| 国产日韩精品一区| 亚洲高清在线观看| 国产伦精品一区二区三区免费| 六月丁香综合| 欧美日本在线| 久久蜜桃精品| 欧美日韩www| 久久精品最新地址| 欧美激情视频在线播放| 久久激情五月激情| 欧美精品三级| 久久噜噜亚洲综合| 欧美视频日韩| 欧美freesex交免费视频| 欧美视频一区在线| 亚洲成人资源网| 国产午夜精品理论片a级大结局| 亚洲高清视频一区| 伊人精品在线| 亚洲欧美日韩网| 在线亚洲免费| 你懂的一区二区| 久久国产精品99国产精| 欧美日韩精品一区二区三区| 男同欧美伦乱| 国产在线精品二区| 亚洲在线成人| 亚洲一区影音先锋| 欧美巨乳在线| 亚洲二区视频| 亚洲福利视频免费观看| 午夜天堂精品久久久久| 亚洲视频欧美视频| 欧美人交a欧美精品| 欧美成人免费在线观看| 国产免费成人av| 欧美日韩免费一区二区三区| 99在线热播精品免费| 久久亚洲一区二区三区四区| 久久精品麻豆| 国产日韩成人精品| 午夜精品国产更新| 亚洲欧美日韩精品久久| 欧美日韩国产一区二区三区| 亚洲欧洲日产国产网站| 亚洲理论电影网| 欧美激情亚洲| 亚洲免费久久| 亚洲综合成人在线| 国产精品社区| 午夜综合激情| 麻豆免费精品视频| 91久久夜色精品国产网站| 欧美v日韩v国产v| 欧美激情按摩| 日韩午夜三级在线| 欧美色一级片| 午夜精品免费在线| 久久一区二区三区国产精品| 在线观看成人网| 欧美成人亚洲| 亚洲乱码日产精品bd| 亚洲欧美日韩精品久久| 国产色产综合产在线视频| 久久精品亚洲国产奇米99| 欧美风情在线观看| 一区二区三区视频在线看| 国产精品美女www爽爽爽视频| 午夜精品在线| 美女日韩在线中文字幕| 日韩亚洲欧美高清| 国产九九精品视频| 久久天天躁狠狠躁夜夜爽蜜月| 欧美高清视频一二三区| 一本久久青青| 国产午夜精品视频| 免播放器亚洲一区| 亚洲五月婷婷| 蜜乳av另类精品一区二区| 一本一本久久| 国内精品福利| 欧美午夜剧场| 另类亚洲自拍| 亚洲免费一在线| 亚洲高清在线观看一区| 欧美一区国产一区| 日韩视频永久免费| 国产婷婷色一区二区三区在线| 女女同性精品视频| 午夜精品免费在线| 99re热这里只有精品视频 | 亚洲精品五月天| 欧美在线电影| 亚洲美女在线观看| 国产专区综合网| 欧美天堂亚洲电影院在线观看 | 国产精品成人va在线观看| 久久久久www| 亚洲一区二区影院| 亚洲精品国产拍免费91在线| 久久久国产精品一区二区三区| 亚洲精品欧洲精品| 国内外成人免费激情在线视频网站| 欧美日韩国产在线播放网站| 久久亚洲美女| 午夜精品亚洲一区二区三区嫩草| 亚洲精品乱码| 91久久精品美女| 欧美精品一区二区三区高清aⅴ| 久久av二区|