Posted on 2008-04-30 16:45
MichaelCao 閱讀(970)
評論(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秒的...
總結:從表面上來看,很明顯的區別是當我們用的是信號量的時候,這個時候是有調度的.因為從運行結果上來看,主線程在創建其他兩個線程后,其他線程開始運行.并且主線程也在運行.但怎么運行這個是無法確定的,這是一個并發的過程.
當使用自旋鎖后,這個就不一樣了.當運行到臨界區的時候,它是直接的過去,不是會產生一個等待,或者一個調度.
不知道編譯器是怎么編譯的.很想知道編譯后二進制代碼有什么區別.但這個好像有點太難....不過我覺得從運行結果上來看這么多,應該差不多了.